Recently, I plan to rewrite my website with go, and I want to find a website that automatically generates API. Everyone unanimously recommends go-swagger, thinking that a library should be easy to use. I was thinking of simple things and just look at the domestic website tutorial. Well, who knew the road would be too bumpy. This article does not talk about the principle of swagger, but only talks about the problems encountered during the installation process, for go Xiaobai.
Most people are talking about go get swagger. In the early days, go get could directly generate binary executable files. My go version is 1.18. Go install probably appeared in go1.16. Go get just got the source code Instead of compiling and generating the corresponding executable file, when I saw other people's swag init, I frantically reported that I didn't know what swag was. My first reaction was that it was not added to the environment variable. I searched swag globally and found that there was no such executable file. This means that no executable file is generated, and I suddenly thought of the go install I saw before. After 1.16, get only gets the source code and does not generate binary files, so if your go version is after 1.16, you should not use go If you get it, you should use go install to generate binary files
go install github.com/swaggo/swag/cmd/swag
Another thing to note is that many blogs import this file, and now it has changed its name. It is not like this. The
solution is to use thisimport swaggerFiles "github.com/swaggo/files"
As for other attributes, just learn it yourself, so I won’t go into details here
Attach a simple version of the code that can run without logic processing. Let me _ "swawgger/docs"
tell you about the package name. swagger is the name of go.mod. This is to import the content of swag init.
package main
import (
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
gs "github.com/swaggo/gin-swagger"
_ "swawgger/docs" // 千万不要忘了导入把你上一步生成的docs
)
// @title API文档
// @version 1.0
// @description 怕自己忘记了,记录一下
// @BasePath /api/v1
// @contact.name dxgzg
// @contact.url http://dxgzg.site
// @contact.email [email protected]
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host 这里写接口服务的host
// @BasePath 这里写base path
func main() {
r := gin.New()
// liwenzhou.com ...
r.GET("/swagger/*any", gs.WrapHandler(swaggerFiles.Handler))
r.Run()
}
// ShowAccount godoc
// @Summary Show an account
// @Description get string by ID
// @Tags accounts
// @Accept json
// @Produce json
// @Param id path int true "Account ID"
// @Router /accounts/{id} [get]
func ShowAccount(ctx *gin.Context) {
}
// second godoc
// @Summary Show an account
// @Description get string by ID
// @Tags accounts
// @Accept json
// @Produce json
// @Param id path int false "Account ID"
// @Router /second/id [get]
// @Success 200 {object} string
func second(ctx *gin.Context) {
}
If you don't know how to use it, it is recommended to read this blog using go-swagger