Gin 是一個 go 寫的 web 框架,具有高性能的優點。官方地址:https://github.com/gin-gonic/gin
先跑一個demo(先安裝gin框架,具體見官方地址):
1.vscode新建文件夾project,文件夾中新建一個go文件,index.go
index.go文件如下:
package main import ( "fmt" "github.com/gin-gonic/gin" ) func main() { r := gin.New() r.Use(gin.Logger()) r.Use(gin.Recovery()) r.GET("/first", func(c *gin.Context) { fmt.Println("first .........") }) authorized := r.Group("/try") authorized.POST("/second", second) authorized.POST("/third", third) // 嵌套路由組 testing := authorized.Group("testing") testing.GET("/forth", fourth) // 監聽並在 0.0.0.0:8080 上啟動服務 r.Run(":8080") } func second(c *gin.Context) { fmt.Println("second .........") } func third(c *gin.Context) { fmt.Println("third .........") } func fourth(c *gin.Context) { fmt.Println("fourth .........") }
2.go中代碼必須在gopath的目錄下運行,為了擺脫這個限制,執行 go mod init project,這時會生成go.mod go.sum兩個文件
3.運行一下這個代碼:go run index.go
上圖可以看到,你的所有路由路徑
4,.利用postman來測試這些鏈接是否能夠正常運行(注意選擇post和get方法)