Go standard library has a lot of useful packages, but for creating routes on your API other packages are a better fit.
In this tutorial we will use gorilla/mux package as our request router.
You can find the documentation for gorilla/mux here
First let’s modify our previous example to have two different handler functions, one for GET and one for POST, instead of one for both requests.
You can find the previous post here.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
func getMembersHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
j, _ := json.Marshal(members)
w.Write(j)
}
func postMembersHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var m Member
b, _ := ioutil.ReadAll(r.Body)
json.Unmarshal(b, &m)
members = append(members, m)
j, _ := json.Marshal(m)
w.Write(j)
}
Now we have two different functions for handling two different requests GET /members and POST /members, the main advantage of this
refactoring is that we can now test the functions separately, I will show you how to test your handler functions on a future post.
And here is the new main() function:
1
2
3
4
5
6
7
8
func main() {
r := mux.NewRouter()
r.HandleFunc("/members", getMembersHandler).Methods("GET")
r.HandleFunc("/members", postMembersHandler).Methods("POST")
http.Handle("/", r)
http.ListenAndServe(":8080", nil)
}
Explanations:
- On line 2 we created a new Router, this is the object that will route our requests to the right handler function.
- On line 3 we are declaring that the
getMembersHandlerfunction will handleGET /membersrequests. - On line 4 we are declaring that the
postMembersHandlerfunction will handlePOST /membersrequests.
The package gorilla/mux has a lot of different ways to match requests, check the documentation for more information.
Here is the full example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/gorilla/mux"
)
var members = []Member{Member{"someuser", "someuser@somedomain.com"}}
type Member struct {
Login string
Email string
}
func getMembersHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
j, _ := json.Marshal(members)
w.Write(j)
}
func postMembersHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var m Member
b, _ := ioutil.ReadAll(r.Body)
json.Unmarshal(b, &m)
members = append(members, m)
j, _ := json.Marshal(m)
w.Write(j)
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/members", getMembersHandler).Methods("GET")
r.HandleFunc("/members", postMembersHandler).Methods("POST")
http.Handle("/", r)
http.ListenAndServe(":8080", nil)
}