gin 結(jié)構(gòu)分析

|-- binding                     將請求的數(shù)據(jù)對象化并校驗(yàn)
|-- examples                 各種列子
|-- json                          提供了另外一種json實(shí)現(xiàn)
|-- render                      渲染

|-- gin.go                      gin引擎
|-- gin_test.go
|-- routes_test.go
|-- context.go                  上下文
|-- context_test.go
|-- response_writer.go          響應(yīng)的數(shù)據(jù)輸出
|-- response_writer_test.go
|-- errors.go                   錯(cuò)誤處理
|-- errors_test.go
|-- tree.go                       路由存儲結(jié)構(gòu)的具體實(shí)現(xiàn)
|-- tree_test.go
|-- routergroup.go           路由組
|-- routergroup_test.go
|-- auth.go                       鑒權(quán)的中間件
|-- auth_test.go
|-- logger.go                    日志中間件
|-- logger_test.go
|-- recovery.go                 崩潰處理中間件
|-- recovery_test.go

|-- mode.go                     應(yīng)用模式
|-- mode_test.go
|-- utils.go                         工具類
|-- utils_test.go

按照字母排序解釋模塊

Bind

內(nèi)置的有json, xml, protobuf, form, query, yaml. 這些Bind極大的減少我們自己去解析各種個(gè)樣的數(shù)據(jù)格式, 提高我們的開發(fā)速度

Bind的實(shí)現(xiàn)都在gin/binding里面. 這些內(nèi)置的Bind都實(shí)現(xiàn)了Binding接口, 主要是Bind()和Name函數(shù).

ginS

This is API experiment for Gin.

package main
import (
    "github.com/gin-gonic/gin"
    "github.com/gin-gonic/gin/ginS"
)
func main() {
    ginS.GET("/", func(c *gin.Context) { c.String(200, "Hello World") })
    ginS.Run()
}

context

HTTP請求上下文處理,使用sync pool實(shí)現(xiàn)
// Context作為一個(gè)數(shù)據(jù)結(jié)構(gòu)在中間件中傳遞本次請求的各種數(shù)據(jù)、管理流程,進(jìn)行響應(yīng)

type Context struct {
    // ServeHTTP的第二個(gè)參數(shù): request
    Request   *http.Request

    // 用來響應(yīng) 
    Writer    ResponseWriter
    writermem responseWriter

    // URL里面的參數(shù),比如:/xx/:id  
    Params   Params
    // 參與的處理者(中間件 + 請求處理者列表)
    handlers HandlersChain
    // 當(dāng)前處理到的handler的下標(biāo)
    index    int8

    // Engine單例
    engine *Engine

    // 在context可以設(shè)置的值
    Keys map[string]interface{}

    // 一系列的錯(cuò)誤
    Errors errorMsgs

    // Accepted defines a list of manually accepted formats for content negotiation.
    Accepted []string
}

gin

gin 是核心部件,https://godoc.org/github.com/gin-gonic/gin#New
初始化 Engine 對象, 關(guān)鍵步驟: 初始化 路由組
初始化 pool, 這是核心步驟. pool 用來存儲 context 上下文對象.trees保存路由信息。

type Engine struct {
    RouterGroup
    RedirectTrailingSlash bool
    RedirectFixedPath bool
    HandleMethodNotAllowed bool
    ForwardedByClientIP    bool
    AppEngine bool
    UseRawPath bool
    UnescapePathValues bool
    MaxMultipartMemory int64
    delims           render.Delims
    secureJsonPrefix string
    HTMLRender       render.HTMLRender
    FuncMap          template.FuncMap
    allNoRoute       HandlersChain
    allNoMethod      HandlersChain
    noRoute          HandlersChain
    noMethod         HandlersChain
    pool             sync.Pool      //保存上下文
    trees            methodTrees // 保存路由信息 采用前綴樹數(shù)據(jù)結(jié)構(gòu)
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容