go get github.com/swaggo/swag/cmd/swag
go get -u github.com/swaggo/gin-swagger go get -u github.com/swaggo/gin-swagger/swaggerFiles
swag init
cannot parse source files /home/wbw/go/src/debuggrpc/test/testswag/main.go: open /home/wbw/go/src/debuggrpc/test/testswag/main.go: no such file or directory
main.go
package main import ( "debuggrpc/test/testswag/controller" "github.com/gin-gonic/gin" "github.com/swaggo/gin-swagger/swaggerFiles" ginSwagger "github.com/swaggo/gin-swagger" _ "debuggrpc/test/testswag/docs" ) // @title TestSwg API // @version 1.0 // @host 127.0.0.1:9501 // @BasePath /testswag/v1.0 func main(){ router := gin.Default() // v1 r1 := router.Group("/testswag/v1.0") { r1.GET("/Baseinfo", controller.Baseinfo) } router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) router.Run( "0.0.0.0:8080") }
base.go
package controller import ( "github.com/gin-gonic/gin" "net/http" ) // @Summary 获取基础信息列表 // @Description 获取基础信息列表 // @Tags 基础信息 // @Accept json // @Success 200 {json} json "{"errno":0,"errmsg": "成功","data":infolist}" // @Failure 400 {json} json "{"errno":4502,"errmsg": "查询失败"}" // @Router /Baseinfo [post] func Baseinfo(ctx *gin.Context) { resp := make(map[string]interface{}) resp["data"] = "baseinfo" resp["errno"] = 200 resp["errmsg"] = "success" ctx.JSON(http.StatusOK, resp) return }
运行main.go之后,浏览器分别访问 http://localhost:8080/testswag/v1.0/Baseinfo
http://localhost:8080/swagger/index.html
原文:https://www.cnblogs.com/bushuwei/p/15137619.html