What is Gin?

overview

Gin is a web framework for the Go language. You can create web applications much more efficiently than using the Go language out of the box.

It’s also performant, with a bundled HttpRouter providing up to 40x faster performance. Its philosophy is inspired by Martini, a similar Go framework with a powerful API.

Gin allows web application development to meet a wide variety of product needs, from microservices to simple API server development. In addition, you can benefit from various features such as routing capabilities, middleware support, and rendering capabilities that support it.

Compare with plain Go language

Frameworks have become an integral part of normal web application development, and we don’t recommend using them plain even in Go.

If you stick to the plane, it is essential to have a corresponding preparedness and learning cost. See the code below.

Another advantage of using Gin is that you can use pre-bundled functions without having to select them.

Plain Go

func Index(w http.ResponseWriter, r *http.Request, _httprouter.Params) {
  fmt.Fprint(w, "Welcome!\n")
}
http. HandleFunc("/users/", Index)

Gin

func(c *gin. Context) {
  c.JSON(200, gin.H{ "message": "Welcome"})
})
router. GET("/", Index)

Even just thinking about sending a simple Get request can be written intuitively and simply using the framework.

And this is part of the function. If the amount of other descriptions increases, plain Go will get even worse.

Routing

Gin has API methods that make routing control easier, and plain Go is different and intuitive.

func main() {
  // Creates a gin router with default middleware:
  // logger and recovery (crash-free) middleware
  router := gin. Default()
  router. GET("/someGet", getting)
  router.POST("/somePost", posting)
  router.PUT("/somePut", putting)
  router.DELETE("/someDelete", deleting)
  router.PATCH("/somePatch", patching)
  router.HEAD("/someHead", head)
  router.OPTIONS("/someOptions", options)
  // By default it serves on :8080 unless a
  // PORT environment variable was defined.
  router. Run()
  // router.Run(":3000") for a hard coded port
}

People who have touched Express.js should be able to understand it the moment they see it.

This clarity is also an advantage of using Gin.

Middleware

Gin also has a function called middleware. This function is guaranteed to fire before and after the API is executed.

Because of its features, it is useful when implementing functions such as session and cookie management, access loggers, etc.

func main() {
// Creates a router without any middleware by default
  r := gin.New()
  // global middleware
  // Logger middleware will write the logs to gin.DefaultWriter even if you set with GIN\_MODE=release.
  // By default gin.DefaultWriter = os.Stdout
  r.Use(gin.Logger())

  // Recovery middleware recovers from any panics and writes a 500 if there was one.
  r.Use(gin.Recovery())

  // Per route middleware, you can add as many as you desire.
  r. GET("/benchmark", MyBenchLogger(), benchEndpoint)

  // Authorization groups
  // authorized := r.Group("/", AuthRequired())
  // exactly the same as:
  authorized := r.Group("/")
  // per group middleware! in this case we use the custom created
  // AuthRequired() middleware just in the "authorized" group.
  authorized.Use(AuthRequired()) {
    authorized. POST("/login", loginEndpoint)
    authorized. POST("/submit", submitEndpoint)
    authorized. POST("/read", readEndpoint)
    // nested groups
    testing := authorized.Group("testing")
    testing. GET("/analytics", analyticsEndpoint)
  }
  // Listen and serve on 0.0.0.0:8080
  r.Run(":8080")
}

summary

Although I briefly explained this time, it also has other powerful functions that support web application development.

Another advantage is that there are many users, so there are many documents and technical blogs.

I think it’s worth giving it a try, even for people who have used Go plainly and who are wondering which framework to use.