Go REST API (Part 1)
Building a REST API in Go
In this post we are going to learn how to build a REST server in Go.
What is REST
REST is an acronym for Representational State Transfer. It is an architectural style for facilitating communications between a stateless client and server. The five basic HTTP verbs used in a REST system are GET, POST, PUT, PATCH and DELETE.
- GET retrieves a specific resource or collection of resources.
- POST creates a new resource.
- PUT replaces an existing resource.
- PATCH updates an existing resource.
- DELETE removes a resource.
These methods generally make up what is referred to as a CRUD system (Create Read Update Delete).
Getting Started
To start setting up our API we’ll start with a new project in GO. Create a new project folder in your GOPATH:
|
|
We will also need to get our router package:
|
|
Creating our server
Next we’ll create our main.go
file.
|
|
Then run it with go run main.go
and fire up a web browser pointed at http://127.0.0.1:3001 and see what we get.
|
|
Adding an endpoint
Uh oh…Looks like we forgot something. Well, we need to tell the server what to listen for. Let’s add an endpoint. To do this we need to use the mux.Router.HandleFunc
method, which takes a string for the path, and a func(http.ResponseWriter, *http.Request)
. Lets add another function to main.go
:
|
|
Then we’ll tell the router to run this function when we send a request to an endpoint:
|
|
Now when we launch the server with go run main.go
and point our browser at http://127.0.0.1:3001/test we see the following:
|
|
Working example
Great! We have a working test endpoint, so lets make it do something actually useful. We’ll create a user management server. We’ll be able to Create, Retrieve, Update, and Delete users.
First we need to add a “database” to store our users. For this example We’ll create a struct user
that will contain an individual user, and use an in-memory map of users. We’ll also initialize the database with the first user “User 1” with an ID of 1 when we start the server.
|
|
Also, lets change our test endpoint to an endpoint called user:
|
|
Better, but we need to be able to specify a user ID for our request. How do we tell the server what user we want to return? Well, we need to add the ID to the endpoint HandleFunc, but at the same time, we want to make sure only valid IDs are passed to our endpoint since our ID above is defined as an int
. To do this we use a regular expression after the variable identifier on the endpoint, and we can retrieve those variables from the request using mux.Vars
|
|
And finally we’ll get the User’s name from the database and return it via the http.ResponseWriter
. Since mux.Vars
returns the ID as a string, we’ll have to convert it to an int
. If we are unable to convert the ID to an int
we’ll send back a 500 Internal Server Error, with the error message from strconv.Atoi
. If the ID provided is not found we’ll want to return a 404 error. We’ll also want to encode our output in a format that is useful on many different remote systems. In this case we’ll use JSON.
|
|
Conclusion
We’ve created a basic server that responds to a GET request from a browser to return a user. In the next part we will look at adding our basic methods to the user endpoint to let us Create, and Delete users.
Here is a gist of our main.go from this post.