Gin源码之函数、类型
本文基于github.com/gin-gonic/gin v1.7.7依赖进行讲解
1.Default函数
创建一个默认的路由引擎,即引导比人访问什么地址,访问什么函数的这样一个过程,需要一个默认的路由引擎取去管理;Default是一个函数,没有参数,它的返回值是一个默认引擎*Engine
func Default() *Engine {
debugPrintWARNINGDefault()
engine := New()
engine.Use(Logger(), Recovery())
return engine
}
2.HandlerFunc函数类型
HandlerFunc 是gin里面的一个自定义类型
// HandlerFunc defines the handler used by gin middleware as return value.
type HandlerFunc func(*Context)
3.H map类型
// H is a shortcut for map[string]interface{}
type H map[string]interface{}
4.BasicAuth函数
// BasicAuth returns a Basic HTTP Authorization middleware. It takes as argument a map[string]string where
// the key is the user name and the value is the password.
func BasicAuth(accounts Accounts) HandlerFunc {
return BasicAuthForRealm(accounts, "")
}
5.Accounts map类型
type Accounts map[string]string
6.AuthUserKey常量
// AuthUserKey is the cookie name for user credential in basic auth.
const AuthUserKey = "user"