1.默认路由实例
router := gin. Default ( )
2. 路由分组
goodsGroup := router. Group ( "/goods" )
{
goodsGroup. Get ( "/list" , goodList)
}
func goodsList ( c * gin. Context) {
c. JSON ( 200 , gin. H{
"name" : "List" ,
} )
}
3. 获取url的参数
goodsGroup := router. Group ( "/goods" )
{
goodsGroup. GET ( "/:id/:action" , goodsDetail)
}
func goodsDetail ( c * gin. Context) {
id := c. Param ( "id" )
action := c. Param ( "action" )
c. JSON ( 200 , gin. H{
"id" : id,
"action" : action,
} )
}
type Person struct {
ID int `uri:"id" binding:"required"`
Name string `uri:"name" binding:"required"`
}
func main ( ) {
router := gin. Default ( )
router. GET ( "/:name/:id" , func ( c * gin. Context) {
var person Person
if err := c. ShouldBindUri ( & person) ; err != nil {
c. Status ( 404 )
return
}
c. JSON ( http. StatusOK, gin. H{
"name" : person. Name,
"id" : person. ID,
} )
} )
router. Run ( )
4. 获取 get和post表单信息
router. POST ( "/post" , getPost)
func getPost ( c * gin. Context) {
id := c. Query ( "id" )
page := c. DefaultQuery ( "page" , "0" )
name := c. PostForm ( "name" )
message := c. DefaultPostForm ( "message" , "信息" )
c. JSON ( http. StatusOK, gin. H{
"id" : id,
"page" : page,
"name" : name,
"message" : message,
} )
}
5. gin返回protobuf
router. GET ( "/someProtoBuf" , returnProto)
func returnProto ( c * gin. Context) {
course := [ ] string { "python" , "go" , "微服务" }
user := & proto. Teacher{
Name: "bobby" ,
Course: course,
}
c. ProtoBuf ( http. StatusOK, user)
}
6. 表单验证
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
type LoginForm struct {
User string `json:"user" binding:"required,min=3,max=10"`
Password string `json:"password" binding:"required"`
}
type SignUpForm struct {
Age uint8 `json:"age" binding:"gte=1,lte=130"`
Name string `json:"name" binding:"required,min=3"`
Email string `json:"email" binding:"required,email"`
Password string `json:"password" binding:"required"`
Repassword string `json:"repassword" binding:"required,eqfield=Password"`
}
func main ( ) {
router := gin. Default ( )
router. POST ( "/loginJSON" , func ( c * gin. Context) {
var loginForm LoginForm
if err := c. ShouldBind ( & loginForm) ; err != nil {
fmt. Println ( err. Error ( ) )
c. JSON ( http. StatusBadRequest, gin. H{
"error" : err. Error ( ) ,
} )
return
}
c. JSON ( http. StatusOK, gin. H{
"msg" : "登录成功" ,
} )
} )
router. POST ( "/signup" , func ( c * gin. Context) {
var signUpForm SignUpForm
if err := c. ShouldBind ( & signUpForm) ; err != nil {
fmt. Println ( err. Error ( ) )
c. JSON ( http. StatusBadRequest, gin. H{
"error" : err. Error ( ) ,
} )
return
}
c. JSON ( http. StatusOK, gin. H{
"msg" : "注册成功" ,
} )
} )
router. Run ( ":8080" )
}
7. 表单验证错误翻译成中文
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/locales/en"
"github.com/go-playground/locales/zh"
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
en_translations "github.com/go-playground/validator/v10/translations/en"
zh_translations "github.com/go-playground/validator/v10/translations/zh"
"net/http"
)
type LoginForm struct {
User string `json:"user" binding:"required,min=3,max=10"`
Password string `json:"password" binding:"required"`
}
type SignUpForm struct {
Age uint8 `json:"age" binding:"gte=1,lte=130"`
Name string `json:"name" binding:"required,min=3"`
Email string `json:"email" binding:"required,email"`
Password string `json:"password" binding:"required"`
Repassword string `json:"repassword" binding:"required,eqfield=Password"`
}
var trans ut. Translator
func InitTrans ( locale string ) ( err error ) {
if v, ok := binding. Validator. Engine ( ) . ( * validator. Validate) ; ok {
zhT := zh. New ( )
enT := en. New ( )
uni := ut. New ( enT, zhT, zhT)
trans, ok = uni. GetTranslator ( locale)
if ! ok {
return fmt. Errorf ( "uni.GetTranslator(%s)" , locale)
}
switch locale {
case "en" :
en_translations. RegisterDefaultTranslations ( v, trans)
case "zh" :
zh_translations. RegisterDefaultTranslations ( v, trans)
default :
en_translations. RegisterDefaultTranslations ( v, trans)
}
return
}
return
}
func main ( ) {
if err := InitTrans ( "zh" ) ; err != nil {
fmt. Println ( "初始化翻译器错误" )
return
}
router := gin. Default ( )
router. POST ( "/loginJSON" , func ( c * gin. Context) {
var loginForm LoginForm
if err := c. ShouldBind ( & loginForm) ; err != nil {
errs, ok := err. ( validator. ValidationErrors)
if ! ok {
c. JSON ( http. StatusBadRequest, gin. H{
"error" : err. Error ( ) ,
} )
}
fmt. Println ( err. Error ( ) )
c. JSON ( http. StatusBadRequest, gin. H{
"error" : errs. Translate ( trans) ,
} )
return
}
c. JSON ( http. StatusOK, gin. H{
"msg" : "登录成功" ,
} )
} )
router. POST ( "/signup" , func ( c * gin. Context) {
var signUpForm SignUpForm
if err := c. ShouldBind ( & signUpForm) ; err != nil {
errs, ok := err. ( validator. ValidationErrors)
if ! ok {
c. JSON ( http. StatusBadRequest, gin. H{
"error" : err. Error ( ) ,
} )
}
fmt. Println ( err. Error ( ) )
c. JSON ( http. StatusBadRequest, gin. H{
"error" : errs. Translate ( trans) ,
} )
return
}
c. JSON ( http. StatusOK, gin. H{
"msg" : "注册成功" ,
} )
} )
router. Run ( ":8080" )
}