一、下載swaggit

$ go get -u github.com/swaggo/swag/cmd/swag

二、在main.go所在目錄執行github

$ swag init

生成docs/doc.go以及docs/swagger.json,docs/swagger.yamljson

三、下載gin-swaggerapi

$ go get -u github.com/swaggo/gin-swagger
$ go get -u github.com/swaggo/files

而後在路由文件引入app

import (
	"github.com/gin-gonic/gin" swaggerFiles "github.com/swaggo/files" "github.com/swaggo/gin-swagger" _ "github.com/swaggo/gin-swagger/example/basic/docs" // docs is generated by Swag CLI, you have to import it. )

並增長swagger訪問路由post

url := ginSwagger.URL("http://localhost:8080/swagger/doc.json") // The url pointing to API definition
    r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))

 

三、一些註解,編寫各API handler方法註釋(註解格式傳送門)url

1)main.go主程序文件註釋:spa

// @title Golang Esign API
// @version 1.0
// @description  Golang api of demo
// @termsOfService http://github.com

// @contact.name API Support
// @contact.url http://www.cnblogs.com
// @contact.email ×××@qq.com

//@host 127.0.0.1:8081
func main() {
}

2)handler方法註釋:egcode

//CreatScene createScene
// @Summary createScene
// @Description createScene
// @Accept multipart/form-data
// @Produce  json
// @Param app_key formData string true "AppKey"
// @Param nonce_str formData string true "NonceStr"
// @Param time_stamp formData string true "TimeStamp"
// @Success 200 {object} app.R
// @Failure 500 {object} app.R
// @Router /dictionaries/createScene [post]