Golang Web Framework Gin (part 1)
Gin is a Web framework written in Go (Golang).
To prepare
- First you need to install Go (version 1.12+ required)
1. Install
- Making the address
https://github.com/gin-gonic/gin
Create a directory called go-gin-test. Then CD goes to the directory
➜ mkdir go-gin-test
➜ cd go-gin-test
Copy the code
We use go Mod as package management for our project.
Initialize the go.mod file
go mod init example.com/m/v2
Copy the code
Gin can be installed using the following Go command
go get -u github.com/gin-gonic/gin
Copy the code
Seeing the following output proves that we have gin installed.
➜ 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-5 e06dd20ab57 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-5 e06dd20ab57 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-0 c34fe9e7dc2 go: google.golang.org/protobuf upgrade = > v1.26.0 go: Downloading golang.org/x/crypto v0.0.0 c34fe9e7dc2-20210322153248-0Copy the code
-
If go GET does not respond for a long time or times out, you are advised to use http://goproxy.cn/ to configure the domestic proxy
2. Hello World
Now let’s write our first Web application
➜ go-gin-test tree-l 3. ├── go. Mod ├─ goCopy the code
Our current directory structure is as shown above
The following
- new
main.go
file
➜ go-gin- Test tree-l 3. ├── go. Mod ├── go. Sum ├── mianCopy the code
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 () // Serve on 0.0.0.0:8080 (for Windows "localhost:8080")}Copy the code
Run go build -o Hello to compile as an executable
➜ go-gin-test go build -o hello
➜ go-gin-test ls
go.mod go.sum hello mian.go
Copy the code
Execute./hello to make our service run
➜ 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 :8080Copy the code
Open a browser check http://localhost:8080/hello
At this point, we have finished building our first Web service
3. Group and encapsulate routers
In actual production activities, business interfaces often need to be split into groups such as /user/XXX and/API /XXX. What should we do with GIN?
We continue with the above project for transformation
- new
routerex
folder - new
router.go
file
➜ go-gin-test tree -L 2. heavy Exercises ── heavy exercises ── heavy exercises ── heavy exercises ── heavy exercises ── heavy exercises ── heavy exercisesCopy the code
Edit the router.go file
Package RouterEx import "github.com/gin-gonic/gin" func InitRouter(g *gin.Engine) { http://localhost:8080/g1/hello1 g1 := g.Group("g1") g1.GET("/hello1", func(c *gin.Context) { c.JSON(200, gin.H{ "msg": "Hello g1",})}) http://localhost:8080/g1/hello1 g2 := g.Group("g2") g2.GET("/hello2", func(c *gin.Context) { c.JSON(200, gin.H{ "msg": "Hello g2", }) }) }Copy the code
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:8080 (for Windows "localhost:8080")}Copy the code
Do the same with go build -o Hello
Run./hello to start the service
Open a browser to check http://localhost:8080/g1/hello1 and http://localhost:8080/g2/hello2
You can see that we have split two different API groups. The registration mode of the router is encapsulated.
Try contacting the above methods
The next content preview:
- Use of gin middleware
- Different methods for obtaining parameters on the POST and GET interfaces
- .
Students who want to obtain engineering can follow the superhero Jim, send gin in the public account, obtain engineering.
If you have any more information or suggestions, you can comment in the comments, or you can follow superhero Jim on my instagram, and I’ll get back to you as soon as I see it.