Home  >  Article  >  Backend Development  >  What can golang gin do?

What can golang gin do?

(*-*)浩
(*-*)浩Original
2019-12-03 10:19:463320browse

What can golang gin do?

#Gin is a golang micro-framework with elegant encapsulation, friendly API, and clear source code comments. Version 1.0 has been released. It is fast, flexible, and fault-tolerant.

In fact, for golang, the dependence of the web framework is much smaller than that of Python, Java and the like. (Recommended learning: go)

’s own net/http is simple enough and its performance is also very good. A framework is more like a collection of commonly used functions or tools. With the help of framework development, not only can you save a lot of time caused by commonly used encapsulation, but it can also help the team's coding style and form standards.

The following is a brief introduction to the usage of Gin.

You need to install it first. The installation is relatively simple, just use go get: the version of

go get gopkg.in/gin-gonic/gin.v1

gin is hosted on the gopkg website. During the installation process, gokpg got stuck. Later, I had to download the corresponding source code from github based on the godep file in gin, and then copy it to the corresponding directory.

Hello World

Using Gin to implement Hello world is very simple. Create a router and then use its Run method:

import (
    "gopkg.in/gin-gonic/gin.v1"
    "net/http"
)
func main(){
    
    router := gin.Default()
    router.GET("/", func(c *gin.Context) {
        c.String(http.StatusOK, "Hello World")
    })
    router.Run(":8000")
}

With just a few lines of code, you can implement a web service. Create a routing handler using gin's Default method. Then bind routing rules and routing functions through HTTP method.

Different from the routing function of the net/http library, gin is encapsulated, encapsulating both request and response into the context of gin.Context. Finally, the Run method of starting the routing listening port is used. Small but complete. Of course, in addition to the GET method, gin also supports commonly used restful methods such as POST, PUT, DELETE, OPTION, etc.

The above is the detailed content of What can golang gin do?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is golang flagNext article:What is golang flag