Swag reads declarative comments in Go source and emits Swagger 2.0 specs plus a docs package for frameworks such as Gin. A CLI formats comments, initializes docs, and supports Docker or binary installs.
Overview
Swag converts structured comment annotations in Go code into Swagger Documentation 2.0 for REST APIs. Plugins and examples cover popular web frameworks so generated specs can feed Swagger UI alongside existing services.
Workflow starts with annotated general API info and per-route operation comments, then runs swag init in the module root to produce a docs folder and docs.go you import at startup. Optional swag fmt normalizes comment formatting similar to go fmt.
The comment format documents parameters, request bodies, responses, security schemes, enums, multipart uploads, markdown descriptions, generics, and model overrides. Gin integration sets Swagger metadata programmatically and wires UI routes after generation.
Install options include go install, Docker, or release binaries. Output types can be narrowed when you need only specific documentation artifacts.
Key features
- Swagger 2.0 generation
- Declarative Go comments
- Gin framework integration
- swag fmt comment formatter
- Security and auth annotations
- Multipart and file upload docs
- Custom type override hooks
- Docker or binary install
Use cases
- Documenting Go REST APIs
- Swagger UI with Gin
- OpenAPI-style team handoffs
- Keeping docs near handlers
- Generating client-facing specs