Features
Automation interface documentation
installation
# 安装swag
go get -u github.com/swaggo/swag/cmd/swag # 安装 gin-swagger go get -u github.com/swaggo/gin-swagger go get -u github.com/swaggo/gin-swagger/swaggerFiles
API to write a comment
Example 1 - Add
// @Summary 新增文章标签 // @Produce json // @Param name query string true "Name" // @Param state query int false "State" // @Param created_by query string false "CreatedBy" // @Success 200 {object} app.Response // @Success 500 {object} app.Response // @Router /api/tags [post] func AddTag(c *gin.Context) { ... }
Edit Example 2
// @Summary 编辑文章标签 // @Produce json // @Param id path int true "ID" // @Param name query string true "Name" // @Param state query int false "State" // @Param modified_by query string false "ModifiedBy" // @Success 200 {object} app.Response // @Success 500 {object} app.Response // @Router /api/tags/{id} [put] func EditTag(c *gin.Context) { ... }
Parameter Description Format
@Param state query int false "State"
Are @Params actual parameter query / path type must be an alias
Routing initialization
package routers import ( ... _ "your_module_name/docs" ... ) // InitRouter initialize routing information func InitRouter() *gin.Engine { ... r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) ... return r }
Form
In the implementation of the project root directory
swag init
After the execution, generate
Interface documentation available online
http://127.0.0.1:8000/swagger/index.html