1 Introduction
Gin
It is angolang
excellent micro-framework, with more elegant and friendly encapsulation , clear source code annotations, fast, flexible, fault- tolerantAPI
and convenient features , etc .; It is simple enough, and its performance is also very good; with the help of framework development, it can not only save a lot of time brought by commonly used packaging, but also help the team's coding style and form norms.golang
web
Python
Java
net/http
2. Install the Gin framework
Prerequisite : Before installing
Gin
the software package , you need to installGo
and set upGo
the workspace , and you need to useGo 1.13
and above versions.2.1 View Go version
go version
2.2 Create a folder
2.3 Using
vscode
Open Folder2.4 Use
go mod
the management project and generatego.mod
files
go mod init Gin
2.5 Download and install Gin
go get -u github.com/gin-goni
If the download fails, you can configure the proxy first.
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
After the download is successful, the following prompt will be displayed and a
go.sum
file will be generated.
2.6 New
main.go
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
// 1.创建路由
r := gin.Default()
// 2.绑定路由规则,执行的函数
// gin.Context,封装了request和response
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "hello World!")
})
// 3.监听端口,默认在 0.0.0.0:8080 启动服务
// Run("里面不指定端口号默认为8000")
r.Run(":8000")
}
Run the project Open http://localhost:8000/
in the browser