1.先下载:swag 使用以下指令
go get -u github.com/swaggo/swag/cmd/swag
2.初始化
swag init
3.下载swagger lris所需文件
go get -u github.com/iris-contrib/swagger
go get -u github.com/iris-contrib/swagger/swaggerFiles
4.在你的项目中引入他们
package main
import (
"github.com/kataras/iris"
"github.com/iris-contrib/swagger"
"github.com/iris-contrib/swagger/swaggerFiles"
_ "./docs" // docs is generated by Swag CLI, you have to import it.
)
// @title Swagger Example API
// @version 1.0
// @description This is a sample server Petstore server.
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host petstore.swagger.io
// @BasePath /v2
func main() {
app := iris.New()
config := &swagger.Config{
URL: "http://localhost:8080/swagger/doc.json", //The url pointing to API definition
}
// use swagger middleware to
app.Get("/swagger/{any:path}", swagger.CustomWrapHandler(config, swaggerFiles.Handler))
app.Run(iris.Addr(":8080"))
}
5.执行go run 然后去浏览器访问
http://localhost:8080/swagger/index.html
6.根据文档编写接口文档http://editor.swagger.io/#/
依据https://github.com/iris-contrib/swagger。