Golden Gate Bridge

Simple API in Golang

Gin Web Framework The fastest full-featured web framework for Go.

Let's jump straight into the code and we'll break it down a little later. To make sure we are both on the same page, we will be using the following block to explain what's going on and spin up a simple go api implimenting a ping endpoint.

  

      package main

      import (
        "net/http"
        "github.com/gin-gonic/gin"
      )

      func main() {
        router := gin.Default()
        router.GET("/ping", func(c *gin.Context) {
          c.JSON(http.StatusOK, gin.H{"message": "pong"})
        })
        router.Run(":8090")
      }
    
  

Cool, now we have a simple go api using gin-gonic that we can ping on our local machine. So what does all this mean? Let's take it from the top. First we need to import a few pacakges. For this small app we will use two, "net/http" and "github.com/gin-gonic/gin". For this implementaion, "net/http" is optional, but we'll use it here to take care of setting http status codes for us.

Now for the fun one, "github.com/gin-gonic/gin", this pacakge is a web framework for go. Gin is fast and highly extensible and a great choice for building RESTful services. First we create a new router using the gin.Default() method. This method returns a new gin router with the default middleware: logger and recovery (crash-free) middleware already attached. Next we define a new route using the router.GET() method. This method takes two arguments, the first is the path of the route defined here as "/ping", and the second is the header content we want returned. This means when we curl the "/ping" endpoint, we will get a json response with the message "pong". Finally we start the server using the router.Run() method. The Run method takes a string argument that defines the port we want to run the server on. Since we are running this on our local machine, we will use the port 8090 so we don't conflict with any other services running on our machine that may be using the default port 80 or 8080.

With all the pieces in place, we can now run our go api and ping the "/ping" endpoint. To do this, we will need to start our go application and we should immediately see the output from the server letting us know it's running.

gin gonic server output

Now that we have our server running, we can use curl to ping the "/ping" endpoint and see the response. The curl command below will return a json response with the message "pong" to our terminal as well as log information about the request to the server. In the image above, we can see that our sever is running and gin received a request to the "/ping" endpoint and returned a 200 status code in less than 40 microseconds.

  

    $ curl http://0.0.0.0:8090/ping -s | jq .
    {
      "message": "pong"
    }
    
  

This was just a quick sample of what we can do with gin-gonic. We can add more routes, middleware, and even use gin's built in error handling to make our api more robust. Before we wrap up, it is worth mentioning that by default gin-gonic runs in debug mode. This means that the server will log all requests to the terminal. This is great for development, but not so much for production. To disable debug mode, export the environment variable GIN_MODE=release before starting the server.

Posts + Projects