DEV Community

Cover image for How to setup Golang router with Gin Gonic and Mux
Cavdy
Cavdy

Posted on • Updated on

How to setup Golang router with Gin Gonic and Mux

In this article, I am going to show you how to set up a router system with Gin Gonic and Mux.

Gin Gonic and Mux are the most popular router for Golang. I assume you reading this already know how to write Golang so I will try to keep this article simple and short. Let’s get to it

MUX

Setting up the router with Mux, we are going to get the mux package

go get -u github.com/gorilla/mux

Inside the main function in the main.go file, we set up our router


 func main() {
   // init router
   router := mux.NewRouter()

   // router handler / endpoints
   router.HandleFunc('/endpoint', endpointOne).Methods("POST")
   router.HandleFunc('/endpoint', endpointTwo).Methods("POST")

   // listen and server the router with the port
   log.Fatal(http.ListenAndServe(":8800", router))
 }

 func endpointOne(w http.ResponseWriter, r *http.Request) {
   log.Println("Endpoint One")
 }

 func endpointTwo(w http.ResponseWriter, r *http.Request) {
   log.Println("Endpoint Two")
 }

In the first line of the main function, we initialize our router in a variable we called router (that is a shorthand form of creating a variable in Golang). We use the router variable which now has access to all the methods Mux created, we use the HandleFunc method to create our endpoints where the first args is the name of the endpoint, the second is the function that endpoint will run and then the Method you want the request to be… It can be a GET, DELETE, PUT or POST like in the example above.
Then finally we listen to our router using the built-in Golang HTTP package, where we pass the port and the router, we then wrap it with log.Fatal to see the response on the terminal.
Below the main function are the functions to our endpoints, the router passed the 2 parameters the function which is from the built-in Golang HTTP package which we can use to create our endpoint, to receive data, query, params from the endpoint request and also sends JSON response back to the user.

Gin Gonic

Setting up the router with Gin Gonic, we need to get the Gin Gonic package

go get -u github.com/gin-gonic/gin

Inside the main function in the main.go file, we set up our router


 func main() {
   // init router
   router := gin.Default()

   // router handler / endpoints
   router.POST('/endpoint', endpointOne)
   router.POST('/endpoint', endpointTwo)

   // listen and server the router with the port
   log.Fatal(router.Run(":8800"))
 }

 func endpointOne(c *gin.Context) {
   log.Println("Endpoint One")
 }

 func endpointTwo(c *gin.Context) {
   log.Println("Endpoint Two")
 }

In the first line of the main function, we initialize our router in a variable we called router. We use the router variable which now has access to all the methods Gin Gonic created, we can use the methods directly as you can in the example above… If you are familiar with NodeJs express, this will look familiar to you, unlike Mux.
Then finally we listen to our router using the Run function created in Gin Gonic package, we then wrap it with log.Fatal to see the response on the terminal.

Below the main function are the functions to our endpoints, the router passed the 1parameter the function which is from the Gin Gonic package which we can use to create our endpoint, to receive data, query, params from the endpoint request and also sends JSON response back to the user.
This is how we can set up our router in Golang using the 2 most popular router package, I use both and I love both. You can use any you want, it is all preference.

Top comments (0)