Get the App
SLTechnology News&Howtos  ›  Development  › 

Installation and use of Go Web Framework gin

Shulou Source: shulou.com Published: 2022-06-03 18:01:31 11月13日 Update

This article mainly introduces "the installation and use of Go Web framework gin". In daily operation, I believe many people have doubts about the installation and use of Go Web framework gin. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "installation and use of Go Web framework gin". Next, please follow the editor to study!

Install gin

You can use go get github.com/gin-gonic/gin directly.

There are a lot of examples in the official README. For example, the simplest example code:

Package mainimport "github.com/gin-gonic/gin" func main () {r: = gin.Default () r.GET ("/ ping", func (c * gin.Context) {c.JSON (200, gin.H {"message": "pong",}) r.Run () / / listen and serve on 0.0.0.0 r.GET 8080}

Routin

Router: = gin.Default () / / default is router with Logger and Recovery middleware: = gin.New () / / routing router.Use without middleware (gin.Logger ()) / / you can use this way to specify middleware router.GET ("/ test", MyMiddleware (), testEndpoint) / / you can also add middleware router.GET ("/ someGet") to a specified route in this way. Getting) / / supports all Restful operations / / routing with parameters router.GET ("/ user/:name", func (c * gin.Context) {name: = c.Param ("name")}) / / optional parameters / wildcard function router.GET ("/ user/:name/*action",...) / / routing grouping v1: = router.Group ("/ v1") {v1.POST ("/ login") LoginEndpoint) v1.POST ("/ submit", submitEndpoint)} v1.Use (AuthRequired ()) {} / / routing grouping specifies middleware separately

Request and response

Request

/ / get the routing parameters. Suppose the route is "/ user/:name" c.Params.ByName ("name") / / get the query parameter c.Query ("name") c.DefaultQuery ("name", "Guest") / / get the form parameter c.PostForm ("name") c.DefaultPostForm ("name")

Parameter binding

Request verification

Response

/ / returns a simple string c.String (200,200, "pong") / / returns JSON data c.JSON (200,200, gin.H {"message": "pong",}) / / redirects c.Redirect (http.StatusMovedPermanently, "https://google.com")")

Middle ware

Custom middleware

BasicAuth middleware

Asynchronous cooperative program

Gin can implement asynchronous tasks with the help of collaborators, but at this point you have to manually copy the context and can only be readable.

Router.GET ("/ async", func (c * gin.Context) {cCp: = c.Copy () go func () {time.Sleep (5 * time.Second) log.Println ("Done! in path" + cCp.Request.URL.Path)} ()) so far, the study on "installation and use of Go Web framework gin" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Middleware routing parameters methods usage framework learning methods more grouping help utility next up and down context code tasks examples functions characters Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL MariaDB Microsoft Redmi Shulou Information