In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.