本文档提供 github.com/darkit/gin 的精简 API 地图。
目标不是列出所有导出项,而是帮你快速定位:
- 核心类型在哪
- 常用方法在哪
- 哪份源码/文档才是最终权威
优先级如下:
- 源码注释与测试
go doc github.com/darkit/gin...- 本文档
常用命令:
go doc github.com/darkit/gin
go doc github.com/darkit/gin.Context
go doc github.com/darkit/gin.Engine
go doc github.com/darkit/gin.Router
go doc github.com/darkit/gin/auth与上游 gin-gonic/gin 的公开面对齐说明,见 docs/gin-upstream-compat.md。
源码:engine.go
关键类型与函数:
type Engine structfunc New(opts ...OptionFunc) *Enginefunc Default(opts ...OptionFunc) *Enginefunc (e *Engine) Router() *Routerfunc (e *Engine) RegexRouter() *RegexRouter(高级接口)func (e *Engine) Run(addr ...string) errorfunc (e *Engine) Shutdown(ctx context.Context) errorfunc (e *Engine) OnStart(hooks ...lifecycle.Hook) *Enginefunc (e *Engine) OnShutdown(hooks ...lifecycle.Hook) *Enginefunc (e *Engine) OnStopped(hooks ...lifecycle.Hook) *Engine
常用配置入口位于:options.go
源码:router.go
关键类型与函数:
type HandlerFunc func(*Context)type Router structfunc (r *Router) GET(path string, handlers ...HandlerFunc) IRoutesfunc (r *Router) POST(path string, handlers ...HandlerFunc) IRoutesfunc (r *Router) Handle(method, path string, handlers ...HandlerFunc) IRoutesfunc (r *Router) Use(handlers ...HandlerFunc) IRoutesfunc (r *Router) UseAny(handlers ...any) IRoutesfunc (r *Router) Group(path string, handlers ...HandlerFunc) *Routerfunc (r *Router) Resource(name string, ctrl ResourceController, opts ...ResourceOption)func (r *Router) CRUD(name string, ctrl ResourceController)func (r *Router) Version(v string) *Routerfunc (r *Router) VersionedAPI(v string, setup func(*Router))func (r *Router) HealthCheck(path ...string)func (r *Router) Liveness(path ...string)func (r *Router) Readiness(checks ...Probe)func (r *Router) Startup(checks ...Probe)func (r *Router) GETDoc(path string, handlers ...HandlerFunc) *SwaggerRouteInfofunc WrapMiddleware(h gin.HandlerFunc) HandlerFunc
源码:regex_router.go
定位:
- 高级 regex 控制接口,不是常规注册首选入口
- 默认应优先通过
Engine/Router的GET/POST/Match/Any直接注册 chi 风格 pattern - 当需要
Match()、Handler()、NotFound()、纯 regexGroup/Use时再使用
关键类型与函数:
type RegexRouter structfunc NewRegexRouter() *RegexRouterfunc (r *RegexRouter) GET(pattern string, handlers ...HandlerFunc)func (r *RegexRouter) POST(pattern string, handlers ...HandlerFunc)func (r *RegexRouter) Match(method, path string) (HandlerFunc, map[string]string, bool)
源码:
context.gocontext_upload.gocontext_export.gocontext_image.gocontext_mask.gocontext_auth.gocontext_websocket.go
高频方法分组:
参数与绑定
- 与上游一致的单一来源取值:
ParamQueryDefaultQueryPostFormDefaultPostForm
- 项目增强的聚合取值:
InputParamInt/ParamInt64/ParamFloat/ParamBoolParamIntE/ParamInt64E/ParamFloatE/ParamBoolEParamTime
BindAndValidateBindJSONOrAbortBindQueryOrAbortParsePaginationParseCursorPagination
说明:
Param(key string) string仅表示路径参数,行为与上游gin.Context.Param一致。Input(key, def...)会按“路径参数 -> query -> form”的顺序统一取值。ParamInt/ParamBool这类增强 helper 也基于Input(...),因此语义是“聚合输入解析”,而不是“只解析路径参数”。
成功响应
SuccessSuccessWithMessageCreatedAcceptedNoContentPaginatedCursorPaginated
错误响应
BadRequestUnauthorizedForbiddenNotFoundConflictValidationErrorInternalErrorTooManyRequestsServiceUnavailableGatewayTimeoutProblemWriteProblemValidationProblem
文件与导出
SaveFileSaveFilesValidateFileStreamFileStreamFileInlineExportExcelExportCSVStreamExcelStreamCSV
其他高频能力
Auth()UpgradeWebSocket(...)Logger()Cache()RequestID()GetBearerToken()IsSecure()TraceID()SpanID()RawBody()WebhookEventID()WebhookSignature()WebhookTimestamp()
源码:
response.gopagination.goupload.go
关键类型:
type Response structtype Pagination structtype ErrorResponse structtype PaginationParams structtype UploadConfig structtype UploadResult struct
目录:auth/
建议入口:
auth/README.mdauth/DESIGN.mdauth/API.md
高频能力:
type AuthContexttype Managerfunc NewManager(...)func NewStpLogic(...)func AuthRequired(...) gin.HandlerFunc
目录:middleware/
建议入口:
middleware/README.mdmiddleware/DESIGN.md
高频中间件:
RequestID()Recovery()Logger()CORS(...)RealIP()Timeout(d)Secure()RateLimit(...)RateLimitByUser(...)RateLimitByKey(...)RateLimitTier(...)Compress(...)Cache(...)CacheIf(...)ETag()Idempotent(...)OTel(service, opts...)Throttle(...)ValidateParam(...)NoCache()
按需查看:
pkg/cache/README.mdpkg/export/README.mdpkg/lifecycle/README.mdpkg/logger/README.mdpkg/mail/README.mdpkg/mask/README.mdpkg/routes/README.mdpkg/sms/README.mdpkg/validator/README.mdpkg/websocket/README.md
如果想确认真实用法,优先看:
examples/basic/main.goexamples/advanced/main.goexamples/auto-register/main.goexamples/cache-demo/main.goexamples/swagger-demo/main.go*_test.go
旧版超长 API 汇编已经移除,因为它容易与当前源码漂移。
如果你需要某个具体方法,请直接使用:
go doc github.com/darkit/gin.Context.<MethodName>