多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
> swagger可以很好的为Api提供文档,并且加强代码的注释能力 [TOC] ## 安装 swag ~~~ go get -u github.com/swaggo/swag/cmd/swag ~~~ > 若 `$GOROOT/bin` 没有加入`$PATH`中,你需要执行将其可执行文件移动到`$GOBIN`下 ~~~ swag -v # 安装成功返回 swag version v1.7.0 ~~~ ## 编写swagger注解 ~~~ package main import ( _ "ginw/docs" "github.com/gin-gonic/gin" "github.com/swaggo/gin-swagger" "github.com/swaggo/gin-swagger/swaggerFiles" "net/http" ) // @Summary 打印测试功能 // @title Swagger Example API // @version 0.0.1 // @description This is a sample server Petstore server. // @BasePath /api/v1 // @Host 127.0.0.1:8080 // @Produce json // @Param name query string true "Name" // @Success 200 {string} json "{"code":200,"data":"name","msg":"ok"}" // @Router /api/v1/haha [get] func Print(context *gin.Context) { var ( name string ) name = context.Query("name") context.JSON(http.StatusOK, gin.H{ "code": http.StatusOK, "msg": "success", "data": name, }) } func main() { var ( route *gin.Engine v1 *gin.RouterGroup ) route = gin.Default() route.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) //gin.SetMode(gin.ReleaseMode) v1 = route.Group("/api/v1") { v1.GET("haha", Print) } route.Run() } ~~~ ## 生成注解文档 > 在项目根目录中执行 ~~~ swag init ~~~ > 访问 http://localhost:8080/swagger/index.html **参考文献** > https://www.bbsmax.com/A/lk5amD4451/ > https://blog.csdn.net/eddycjy/article/details/106740800