Skip to content

pgrunde/go-http-primer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 

Repository files navigation

go-http

Get it Go-ing

Gopher puns!

Get that sweet installation of Go, and bookmark Effective Go for later reading and A Tour of Go for later learning. Also the official Getting started with Go section of the wiki is a good place to, well, get started.

Gopath

Hey gopath yourself buddy!

The Gopath environment variable determines the path of your workspace. Right out the gates I have to warn you- Go has an opinion about everything. This includes your directory structure, your linting, your formatting, your test file, you name it. Now if you're the kinda folk who will balk at some language waltzing in here and having the gall to tell you how to write your code, I implore you to reconsider. Go code is formatted and linted the same nearly everywhere, making code more familiar even when you've never seen it.

Let us test that our installation of Go is correct with a quick and simple hello, world.

With the path setup and your workspace ready, be sure you understand what is happening in the bin, pkg and src folders- bin is like Unix bin in that it holds your executables, package is where you keep all your installed packages, and source is the source of your code.

JavaScript differences

Alright you've probably already started to notice some of the differences between JS and Go. Lets talk about the differences.

Thing Go JS
Strongly Typed Yes No
Compiled Yes No
Language Runs in Browser No Yes
Did I leave Go with the extra yes as pro-Golang propaganda? You decide, dear reader, but no.

Simple Server

So one cool thing to know about Go is that is has an awesome standard library of packages and a built in HTTP package that includes its own testing toolkit. That toolkit is used with Go's built in testing framework. Were going to build an example web app with that HTTP package.

The first response

Here on this commit we can see the beginning of a baby server, just born into the world and learning to respond to requests. Express users might recognize the format of an incoming request and an outgoing response. Babies might recognize some of the letters (if they're really bright and have good eyes). Either way, congratulations on your first Go web app! Execute the program with go run main.go.

Serve up files

Responding with a static template is all well and good, but what if I want to serve up some files? Check out the directory of served files we added here. Now I've got my HTML, my CSS, and my JavaScript all together. Hot dog.

Insert your values here

Now we've added templates as proper html files. We're still just doing string interpolation, but now the templates live in their own happy home.

What about those other URL values?

The incoming request has all the goodies you need to bag yourself some URL parameters in this example. Now you can specify things like ?color=%234e73c0 or ?color=blue if you're feeling retro. So far I'm only choosing to render my path and params, but you REST assured you can make all kinds of delicious decisions out of the combinations of those two things.

Make Posts, Not War

Now I've gone and shown you what a form post might look like (also notice I remembered to run go fmt this time). While I'm only having the server print out the form values, I bet you could imagine a website where they did other things. Think of something else you submit to a website? Which thing you submit is your faaaaavorite thing? I should mention that the pattern you're seeing here where I switch on the request method to determine actions taken is a very simple way to solve the muxing problem. There are tools out there which you can use to solve this problem more elegantly like Naoya Inada's denco or Julien Schmidt's httprouter.

JavaScript Object Notation Nation

It is super practical to serve up information from a server that comes in a JSON payload, and here we can see just that. Structs and Maps are all hunky-dory when it comes to JSON marshalling. See if you can't swap out the json.Marshal call and replace it with a Decoder type that decodes the JSON.

Well that's it for now- if you need a quick reference I've found Go By Example to be an invaluable resource for remembering how to do things.

About

Primer on http basics in Go

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published