Automatically generate RESTful API documentation with Swagger 2.0 for Go.
Automatically generate RESTful API documentation with Swagger 2.0 for Go.
English ∙ 简体中文 ∙ Português Swag converts Go annotations to Swagger Documentation 2.0. We've created a variety of plugins for popular Go web frameworks. This allows you to quickly integrate with an existing Go project (using Swagger UI).
Add comments to your API source code, See Declarative Comments Format.
Install swag by using:
go install github.com/swaggo/swag/cmd/swag@latestTo build from source you need Go (1.19 or newer).
Alternatively you can run the docker image:
docker run --rm -v $(pwd):/code ghcr.io/swaggo/swag:latestOr download a pre-compiled binary from the release page.
swag init in the project's root folder which contains the main.go file. This will parse your comments and generate the required files (docs folder and docs/docs.go).swag init Make sure to import the generated docs/docs.go so that your specific configuration gets init'ed. If your General API annotations do not live in main.go, you can let swag know with -g flag.
import _ "example-module-name/docs"swag init -g http/api.goswag fmt format the SWAG comment. (Please upgrade to the latest version)swag fmt…swag fmt -h
NAME:
swag fmt - format swag comments
USAGE:
swag fmt [command options] [arguments...]
OPTIONS:
--dir value, -d value Directories you want to parse,comma separated and general-info file must be in the first one (default: "./")
--exclude value Exclude directories and files when searching, comma separated
--generalInfo value, -g value Go file path in which 'swagger general API Info' is written (default: "main.go")
--help, -h show help (default: false)
Find the example source code here.
Finish the steps in Getting started
swag init to generate Swagger 2.0 docs, import the following packages:import "github.com/swaggo/gin-swagger" // gin-swagger middleware
import "github.com/swaggo/files" // swagger embed filesmain.go code:…Additionally some general API info can be set dynamically. The generated code package docs exports SwaggerInfo variable which we can use to set the title, description, version, host and base path programmatically. Example using Gin:
…controller code…swag initThe Swag Comments can be automatically formatted, just like 'go fmt'. Find the result of formatting here.
Usage:
swag fmtExclude folder:
swag fmt -d ./ --exclude ./internalWhen using swag fmt, you need to ensure that you have a doc comment for the function to ensure correct formatting.
This is due to swag fmt indenting swag comments with tabs, which is only allowed after a standard doc comment.
For example, use
// ListAccounts lists all existing accounts
//
// @Summary List accounts
// @Description get accounts
// @Tags accounts
// @Accept json
// @Produce json
// @Param q query string false "name search by q" Format(email)
// @Success 200 {array} model.Account
// @Failure 400 {object} httputil.HTTPError
// @Failure 404 {object} httputil.HTTPError
// @Failure 500 {object} httputil.HTTPError
// @Router /accounts [get]
func (c *Controller) ListAccounts(ctx *gin.Context) {Example celler/main.go
| annotation | description | example |
|---|---|---|
| title | Required. The title of the application. | // @title Swagger Example API |
| version | Required. Provides the version of the application API. | // @version 1.0 |
| description | A short description of the application. | // @description This is a sample server celler server. |
| tag.name | Name of a tag. | // @tag.name This is the name of the tag |
| tag.description | Description of the tag | // @tag.description Cool Description |
| tag.docs.url | Url of the external Documentation of the tag | // @tag.docs.url https://example.com |
| tag.docs.description | Description of the external Documentation of the tag | // @tag.docs.description Best example documentation |
| termsOfService | The Terms of Service for the API. | // @termsOfService http://swagger.io/terms/ |
| contact.name | The contact information for the exposed API. | // @contact.name API Support |
| contact.url | The URL pointing to the contact information. MUST be in the format of a URL. | // @contact.url http://www.swagger.io/support |
| contact.email | The email address of the contact person/organization. MUST be in the format of an email address. | // @contact.email [email protected] |
| license.name | Required. The license name used for the API. | // @license.name Apache 2.0 |
| license.url | A URL to the license used for the API. MUST be in the format of a URL. | // @license.url http://www.apache.org/licenses/LICENSE-2.0.html |
| host | The host (name or ip) serving the API. | // @host localhost:8080 |
| BasePath | The base path on which the API is served. | // @BasePath /api/v1 |
| accept | A list of MIME types the APIs can consume. Note that Accept only affects operations with a request body, such as POST, PUT and PATCH. Value MUST be as described under Mime Types. | // @accept json |
| produce | A list of MIME types the APIs can produce. Value MUST be as described under Mime Types. | // @produce json |
| query.collection.format | The default collection(array) param format in query,enums:csv,multi,pipes,tsv,ssv. If not set, csv is the default. | // @query.collection.format multi |
| schemes | The transfer protocol for the operation that separated by spaces. | // @schemes http https |
| externalDocs.description | Description of the external document. | // @externalDocs.description OpenAPI |
| externalDocs.url | URL of the external document. | // @externalDocs.url https://swagger.io/resources/open-api/ |
| x-name | The extension key, must be start by x- and take only json value | // @x-example-key {"key": "value"} |
When a short string in your documentation is insufficient, or you need images, code examples and things like that you may want to use markdown descriptions. In order to use markdown descriptions use the following annotations.
| annotation | description | example |
|---|---|---|
| title | Required. The title of the application. | // @title Swagger Example API |
| version | Required. Provides the version of the application API. | // @version 1.0 |
| description.markdown | A short description of the application. Parsed from the api.md file. This is an alternative to @description | // @description.markdown No value needed, this parses the description from api.md |
| tag.name | Name of a tag. | // @tag.name This is the name of the tag |
| tag.description.markdown | Description of the tag this is an alternative to tag.description. The description will be read from a file named like tagname.md | // @tag.description.markdown |
| tag.x-name | The extension key, must be start by x- and take only string value | // @x-example-key value |
Example celler/controller
| annotation | description |
|---|
Why not support openapi 3.x
swag internal repository Unregister / Clear methods
[v2] Empty object body schema generated when using @Accept
[Go 1.27] Support generic methods (methods declaring their own type parameters)
`@Tags` with empty values generates empty-string tags in Swagger output
[v2]: Support `examples` keywords
v2: every @Param body request body wrapped in a spurious 2-branch oneOf with a $ref sibling
v2 --v3.1: panic (nil pointer dereference) on query object fields of named types
Documentation sorted by Tags
v2: no guard against duplicate operationId when one handler is documented on multiple @Router lines