Introduction
HttpRouter
is a popular open source library for HTTP Handler in Golang. This HttpRouter
is famous for its speed and is very minimalist or simple because it only has features for routing, it doesn’t have any features other than that. If you want to see more details, you can visit the github here https://github.com/julienschmidt/httprouter
HTTP Method
The router in the library here is the same as ServeMux
where we can add routes to the Router. The advantage compared to ServeMux is that in this library we can determine the HTTP methods we want to use, for example GET
, POST
, PUT
, DELETE
and so on. The way to add the router to the router is by calling the function which we will use the HTTP method for example router.GET()
.
Httprouter.Handle
When we use ServeMux, of course we add a route to be able to add http.Handler, so this library is different, namely on the router we don’t use http.Handler anymore, but instead we use the httprouter.Handle type. The difference is that there is a third parameter, namely Params
, which we can use for our own needs.
Routers
The HttpRouter
library contains the Router
struct which is an implementation of http.Handler
so we can easily add it to http.Server
to create a Router using the httprouter.New()
function which will return a Router pointers.
The following is an example of router initialization.
main package
import (
"net/http"
"github.com/julienschmidt/httprouter"
)
func main() {
router := httprouter.New()
server := http.Server{
Handlers: routers,
Addr: "localhost:8080",
}
server.ListenAndServe()
}
Create a New Project
OK, now we will try to use this HttpRouter
with a new project first
Create a new folder or new project with mkdir learn-golang-httprouter
. Initialize the Golang module with the command
go mod init github.com/santekno/learn-golang-httprouter
After that, a file called go.mod
will automatically be created. Or if you want it to be easier, you can just clone or download the Santekno repository here https://github.com/santekno/learn-golang-httprouter.
Next, we add the Golang library that we discussed above, namely with this command
go get github.com/julienschmidt/httprouter
If it has been executed, the go.mod
file will add its library to the file like this and a go.sum
file will automatically be created.
module github.com/santekno/learn-golang-httprouter
go 1.21.1
require github.com/julienschmidt/httprouter v1.3.0 // indirect
Don’t forget that we also need an additional library to create unit tests, namely by using this library.
go get github.com/stretchr/testify
As a result, the final contents of the go.mod
file will look like this.
module github.com/santekno/learn-golang-httprouter
go 1.21.1
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/julienschmidt/httprouter v1.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
We continue to create the main.go
file with the initialization contents of httprouter
like this
main package
import (
"fmt"
"net/http"
"github.com/julienschmidt/httprouter"
)
func main() {
router := httprouter.New()
router.GET("/", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
fmt.Fprint(w, "Hello Get")
})
router.POST("/", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
fmt.Fprint(w, "Hello Post")
})
server := http.Server{
Handlers: routers,
Addr: "localhost:8080",
}
server.ListenAndServe()
}
If it’s like this then we can try running this project with the command below
go build && ./learn-golang-httprouter
So if we access it using curl
the GET
method will look like this
➜ santekno-hugo git:(main) ✗ curl http://localhost:8080/
Hello Get%
And for endpoints with a POST
method like this.
➜ santekno-hugo git:(main) ✗ curl --location --request POST 'http://localhost:8080'
Hello Post%