1. 介紹
- Gin是一個golang的微框架,封裝比較優雅,API友好,源碼注釋比較明確,具有快速靈活,容錯方便等特點
- 對於golang而言,web框架的依賴要遠比Python,Java之類的要小。自身的net/http足夠簡單,性能也非常不錯
- 借助框架開發,不僅可以省去很多常用的封裝帶來的時間,也有助於團隊的編碼風格和形成規范
beego和gin的區別:
(1) MVC:Beego支持完整的MVC, Gin不支持完整的MVC(需要開發者自己實現MVC);
(2) 路由&Session:Beego支持正則路由, Gin不支持正則路由;Beego支持Session,Gin不支持Session(需要安裝另外的包);安裝session,推薦包:github.com/astaxie/session;
(3) 適用場景:在業務更加復雜的項目中,適用Beego;在需要快速開發的項目中,適用Beego;在1.0項目中,適用Beego;
(4) Gin在性能方面較Beego更好:當某個接口的性能遭到較大挑戰的時候,考慮使用Gin重寫接口;如果項目的規模不大,業務相對簡單,使用Gin;
2. 安裝
要安裝Gin軟件包,您需要安裝Go並首先設置Go工作區。
1.首先需要安裝Go(需要1.10+版本),然后可以使用下面的Go命令安裝Gin。
go get -u github.com/gin-gonic/gin
2.將其導入您的代碼中:
import "github.com/gin-gonic/gin"
3.(可選)導入net/http。例如,如果使用常量,則需要這樣做http.StatusOK。
import "net/http"
可能遇到的問題補充:
1.golang.org/x/*無法訪問的解決方案:
golang.org這個域名在國內是訪問不到的,導致我們使用golang時,有些依賴包下載不了,好在現在,golang把這些官方依賴包都放在了github上面;
對應的github地址是:https://github.com/golang;
解決方法就是把github上這些項目都一個個git clone 下來,然后放在$GOPATH/golang/x下面,這樣就可以直接使用了;
2.解決Golang獲取google.golang.org/protobuf包報錯的問題
需要對應下載。這里具體的操作方法是,從https://github.com/protocolbuffers/protobuf-go/tree/master/ 下載包,放到%GOPATH%\google.golang.org\protobuf下就可以;
mkdir google.golang.org cd google.golang.org git clone https://github.com/protocolbuffers/protobuf-go.git mv protobuf-go protobuf
3. 使用git clone命令,無法clone成功,出現github|fatal:unable to access|OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com 443 字段:
解決的關鍵因素就是http.sslVerify 的配置需要設置為false,
打開git bash, 敲命令"git config --global http.sslVerify false",搞定;
3. 測試使用
創建一個測試目錄:gin_test
go mod init gin_test go mod edit -require github.com/gin-gonic/gin@latest go mod tidy
3.1.輸出hello,world:
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.監聽端口,默認在8080 // Run("里面不指定端口號默認為8080") r.Run(":8000") }
啟動運行:
go run main.go
輸出結果:
3.2.json輸出返回:
//eg2:輸出json r.GET("/json", func(c *gin.Context) { c.JSON(200, gin.H{ "Example": "Hello Gin", }) })
訪問輸出結果:
3.3.自定義文件請求:
創建routers、controllers目錄:
注冊路由文件:
package routers import ( . "gin_test/controllers" "github.com/gin-gonic/gin" "net/http" ) func RegisterRoutes() *gin.Engine { // 1.創建路由 router := gin.Default() // 2.綁定路由規則,執行的函數 // gin.Context,封裝了request和response router.GET("/", func(c *gin.Context) { c.String(http.StatusOK, "hello World!") }) //eg2:輸出json router.GET("/json", func(c *gin.Context) { c.JSON(200, gin.H{ "Example": "Hello Gin", }) }) //eg3:自定義方法請求 router.GET("/cont/action", Action) return router }
自定義操作方法文件:
package controllers import ( "github.com/gin-gonic/gin" "net/http" "strings" ) //名稱 func Action(c *gin.Context) { name := c.Query("name") action := c.Query("action") //截取/ action = strings.Trim(action, "/") c.String(http.StatusOK, name+" is "+action) }
Main.go文件調整:
package main import ( "gin_test/routers" ) func main() { router := routers.RegisterRoutes() // 綁定端口是8088 router.Run(":8088") }
重新運行啟動:
請求輸出對應結果:
其余操作根據需要去編寫即可。