【go第三方框架系列】gin源码分析第一版

demo源码

package main

import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
)

type User struct {
Id int
Name string
Age int
}
func main() {
defaultServer:=gin.New();
defaultServer.RouterGroup.GET("/",defaultHandeler)
v1 := defaultServer.Group("/v1")
v1.GET("/ping",v1pingHandeler)

defaultServer.Run(":8083")
}


func defaultHandeler(ctx *gin.Context) {
ctx.JSON(200,gin.H{
"status":200,
"success":true,
"data":"{}",
})
}
func v1pingHandeler(context *gin.Context) {
user:=User{1,"golang",20}
data,_:=json.Marshal(user)
fmt.Println(user,data)
context.JSON(200,gin.H{
"status":200,
"success":true,
"data":string(data),
})
//context.Redirect(http.StatusMovedPermanently,"https://github.com/gin-goinc/gin")
}

时序图