1.03 Web框架概览-GIN框架分析
PART1. 基本使用
package gin
import "github.com/gin-gonic/gin"
type UserController struct {
}
func (c *UserController) GetUser(ctx *gin.Context) {
ctx.String(200, "hello, world")
}package gin
import (
"github.com/gin-gonic/gin"
"net/http"
"testing"
)
func TestUserController_GetUser(t *testing.T) {
g := gin.Default()
ctrl := &UserController{}
g.GET("/user", ctrl.GetUser)
g.POST("/user", func(ctx *gin.Context) {
ctx.String(http.StatusOK, "hello %s", "world")
})
g.GET("/static", func(context *gin.Context) {
// 读文件
// 写响应
})
_ = g.Run(":8082")
}PART2. IRoutes接口
PART3. Engine的实现
3.1 基本概念
3.2 Engine是接口http.Handler的实现
3.3 Engine中的tree字段--methodTrees和methodTree
3.4 HandlerFunc和HandlersChain

PART4. Context抽象

PART5. 核心抽象总结

Last updated