跳至主要内容

Gin Gonic 學習筆記

· 閱讀時間約 1 分鐘
Ckai

Gin Gonic 是非常流行的網站框架,其他選擇有 Beggo, BuffaloEcho, Echo, Fiber, Martini, Revel。 另外也有僅提供 HTTP Router 功能的套件,如 Chi, FastHttp, Gorilla Mux, HttpRouter 。

GitHub gin-gonic
官方文件

gin 安裝指令
$ go get -u github.com/gin-gonic/gin

Gin 與資料驗證

官方文件:Model binding and validation
One of

資料驗證範例代碼
// 在 struct 型別後方設定 tag,required、oneof、gt... 是不同的驗證條件。
type requestForm struct {
item string `json:"item" binding:"required, oneof=apple coconut"`
amount int `json:"amount" binding:"required, oneof=1 2 3, gt=0"`
}