title | keywords | description | ||||
---|---|---|---|---|---|---|
Swagger |
|
Generate Swagger documentation for your application. |
This project demonstrates how to integrate Swagger for API documentation in a Go application.
Ensure you have the following installed:
- Golang
- Swag for generating Swagger docs
-
Clone the repository:
git clone https://github.com/gofiber/recipes.git cd recipes/swagger
-
Install dependencies:
go get -u github.com/swaggo/swag/cmd/swag go get -u github.com/swaggo/gin-swagger go get -u github.com/swaggo/files
- Generate the Swagger documentation:
swag init
-
Start the application:
go run main.go
-
Access the Swagger UI: Open your browser and navigate to
http://localhost:8080/swagger/index.html
Here is an example of how to document an API endpoint using Swag:
// @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
}