Let’s create a REST api using golang. In our example, we’ll walk through what’s required to make an API for a Todo-style application.
Starting off
First up, we’re going to create a project. I’ve called mine “todo”.
This gives us a project folder. Start off editing your main.go file. We’ll pop the whole application into this single file, as it’ll be simple enough.
The Server
We can turn our console application now into a server application pretty easily with the net/http module. Once we import this, we’ll use the ListenAndServe function to stand a server up. While we’re at it, we’ll create a NotImplementedHandler so we can assertivly tell our calling clients that we haven’t done anything just yet.
Testing this service will be a little pointless, but we can see our 501’s being thrown:
Routing
Routing will allow us to direct a user’s request to the correct piece of functionality. Routing also helps us extract input parameters for requests. Using mux from gorilla we can quickly setup the list, create, update and delete endpoints we need to accomplish our TODO application.
What’s nice about this, is that our actual routes are what will emit the 501. Anything that completely misses the router will result in a much more accurate 404. Perfect.
Handlers
We can give the server some handlers now. A handler takes the common shape of:
The http.ResponseWriter typed w parameter is what we’ll use to send a payload back to the client. r takes the form of the request, and it’s what we’ll use as an input to the process. This is all looking very “server’s output as a function of its input” to me.
Which means that our router (whilst still unimplemented) starts to make a little more sense.
Modelling data
We need to start modelling this data so that we can prepare an API to work with it. The following type declaration creates a structure that will define our todo item:
Note the json directives at the end of each of the members in the structure. This is allowing us to control how the member is represented as an encoded JSON value. A more idomatic JSON has lowercased member names.
The “database” that our API will manage is a slice.
Implementation
To “list” out todo items, we simply return the encoded slice.
Creating an item is a bit more complex due to value marshalling.
In order to implement a delete function, we need a Filter implementation that knows about Todo objects.
We then add a reference to strconv because we’ll need Atoi to take in the stringid and convert it to an int. Remember, the Id attribute of our Todo object is an int.
Finally, an update. We’ll do the same thing as a DELETE, but we’ll swap the posted object in.
The UpdateTodoHandler appears to be a mix of the delete action as well as create.
Up and running
You’re just about done. The Todo api is doing what we’ve asked it to do. The only thing left now, is to get some logging going. We’ll do that with some clever middleware again, from gorilla that will do just that.
This now gives us a status on requests hitting our server.
That’s all
That’s all for now. The full source is available as a gist.