Swagger API 文档
本项目演示了如何在 Go 应用中集成 Swagger 生成 API 文档。
前提条件
确保你已安装以下内容
- Golang
- Swag 用于生成 Swagger 文档
设置
-
克隆仓库
git clone https://github.com/gofiber/recipes.git
cd recipes/swagger -
安装依赖
go get -u github.com/swaggo/swag/cmd/swag
go get -u github.com/swaggo/gin-swagger
go get -u github.com/swaggo/files
生成 Swagger 文档
- 生成 Swagger 文档
swag init
运行应用
-
启动应用
go run main.go
-
访问 Swagger UI:打开你的浏览器并导航至
http://localhost:8080/swagger/index.html
示例
以下是使用 Swag 文档化 API 端点的示例
// @Summary Show an account
// @Description get string by ID
// @ID get-string-by-int
// @Accept json
// @Produce json
// @Param id path int true "Account ID"
// @Success 200 {object} model.Account
// @Failure 400 {object} http.Response
// @Failure 404 {object} http.Response
// @Router /accounts/{id} [get]
func GetAccount(c *gin.Context) {
// Your code here
}