In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use golang web framework Gin", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use golang web framework Gin" bar!
Gin is a Web framework written in Go (Golang). To prepare, you first need to install Go (version 1.12 +) 1. Install github address https://github.com/gin-gonic/gin
Create a new directory go-gin-test. Then cd enters the directory
➜mkdir go-gin-test
➜cd go-gin-test
We use go mod as the package management for our project.
Initialize the go.mod file
Go mod init example.com/m/v2
You can install Gin using the following Go command
Go get-u github.com/gin-gonic/gin
See the following output to prove that we have installed gin.
➜go-gin-test go get-u github.com/gin-gonic/gin
Go: github.com/gin-gonic/gin upgrade = > v1.6.3
Go: gopkg.in/yaml.v2 upgrade = > v2.4.0
Go: github.com/golang/protobuf upgrade = > v1.5.2
Go: github.com/modern-go/reflect2 upgrade = > v1.0.1
Go: github.com/modern-go/concurrent upgrade = > v0.0.0-20180306012644-bacd9c7ef1dd
Go: github.com/ugorji/go/codec upgrade = > v1.2.5
Go: golang.org/x/sys upgrade = > v0.0.0-20210403161142-5e06dd20ab57
Go: github.com/go-playground/validator/v10 upgrade = > v10.4.2
Go: github.com/json-iterator/go upgrade = > v1.1.10
Go: github.com/leodido/go-urn upgrade = > v1.2.1
Go: downloading github.com/golang/protobuf v1.5.2
Go: downloading golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57
Go: downloading github.com/go-playground/validator/v10 v10.4.2
Go: downloading github.com/ugorji/go v1.2.5
Go: downloading google.golang.org/protobuf v1.26.0
Go: downloading github.com/ugorji/go/codec v1.2.5
Go: golang.org/x/crypto upgrade = > v0.0.0-20210322153248-0c34fe9e7dc2
Go: google.golang.org/protobuf upgrade = > v1.26.0
Go: downloading golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
If go get does not respond for a long time or there is a timeout, it is recommended to use http://goproxy.cn/ to configure domestic agent 2. Hello World
Now let's write our first web program.
➜go-gin-test tree-L 3
.
├── go.mod
└── go.sum
At present, our directory structure is shown above
Next
Create a new main.go file ➜go-gin-test tree-L 3
.
├── go.mod
├── go.sum
├── mian.go
First, let's edit the main.go file
Package main
Import "github.com/gin-gonic/gin"
Func main () {
R: = gin.Default ()
R.GET ("/ hello", func (c * gin.Context) {
C.JSON (200, gin.H {
"message": "hello world"
})
})
R.Run () / listen and serve on 0.0.0.0 for windows 8080 (for windows "localhost:8080")
}
Execute go build-o hello to compile to an executable file
➜go-gin-test go build-o hello
➜go-gin-test ls
Go.mod go.sum hello mian.go
Execute. / hello to get our service running
➜go-gin-test. / hello
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
-using env: export GIN_MODE=release
-using code: gin.SetMode (gin.ReleaseMode)
[GIN-debug] GET / hello-- > main.main.func1 (3 handlers)
[GIN-debug] Environment variable PORT is undefined. Using port: 8080 by default
[GIN-debug] Listening and serving HTTP on: 8080
Open a browser to view http://localhost:8080/hello
Image.png
At this point, we have completed the construction of the first web service
3. Grouping and encapsulation of Router
In actual production activities, business interfaces usually need to be split into many groups, such as / user/XXX, / api/XXX, so what should we do with gin?
We continue with the above project to carry out the transformation.
New routerex folder New router.go file ➜go-gin-test tree-L 3
.
├── go.mod
├── go.sum
├── hello
├── mian.go
└── routerex
└── router.go
Edit the router.go file
Package routerex
Import "github.com/gin-gonic/gin"
Func InitRouter (g * gin.Engine) {
/ / the first set of api APIs for example: http://localhost:8080/g1/hello1
G1: = g.Group ("G1")
G1.GET ("/ hello1", func (c * gin.Context) {
C.JSON (200, gin.H {
"msg": "Hello G1"
})
})
/ / the second set of api APIs for example: http://localhost:8080/g1/hello1
G2: = g.Group ("G2")
G2.GET ("/ hello2", func (c * gin.Context) {
C.JSON (200, gin.H {
"msg": "Hello G2"
})
})
}
Edit the main.go file
Package main
Import (
"example.com/m/v2/routerex"
"github.com/gin-gonic/gin"
)
Func main () {
R: = gin.Default ()
Routerex.InitRouter (r)
R.Run () / listen and serve on 0.0.0.0 for windows 8080 (for windows "localhost:8080")
}
Do the same go build-o hello compilation
Execute. / hello to start the service
Open a browser to view http://localhost:8080/g1/hello1 and http://localhost:8080/g2/hello2
Image.pngimage.png
You can see that we have split two different api groups. And encapsulate the registration mode of router.
You can try to contact the above methods
The next issue will be announced in advance:
Gin middleware uses post and get interfaces to obtain parameters in different ways.
Students who want to get the project can follow the superhero Jim and send gin in the official account to get the project.
If you have more information or any good suggestions, you can reply in the comments, or follow my official account superhero Jim, leave a message on the official account, and reply as soon as I see it.
Thank you for your reading, the above is the content of "how to use golang web framework Gin", after the study of this article, I believe you have a deeper understanding of how to use golang web framework Gin, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.