
swag
Generates Swagger 2.0 API docs for Go projects from code comments.
- Stars
- 13k
- Forks
- 1.5k
- Licence
- MIT
- Language
- Go
- Last update
- 1 months ago
- Type
- Dev tool
What it is
swag reads special comments (annotations) you write above your Go API handlers and produces Swagger 2.0 documentation files, such as swagger.json and swagger.yaml, plus a docs.go file. The generated docs can be shown in Swagger UI so people can browse and test your API. It is used to keep API documentation close to the code it describes.
- Add annotations to Go handler functions and run swag init to generate docs
- Serve generated docs in a Gin app using the gin-swagger plugin named in the README
- Filter which APIs are documented with the --tags option
- Format annotation comments with swag fmt
Great for
- Go developers who already have a REST API using Gin, Echo, net/http, Fiber, or another listed framework
- Teams that want Swagger UI docs generated from code comments rather than written by hand
- Projects that need Swagger 2.0 output specifically
Not ideal for
- Projects that need OpenAPI 3.x output, since the README describes Swagger 2.0 generation
- Non-Go projects, since it parses Go source code
- Teams that prefer writing the API spec by hand as a separate file
How to get it
- 1
Install with a package manager
You'll need: Go 1.19 or newer (only if building from source or using go install)
Install swag with Go
go install github.com/swaggo/swag/cmd/swag@latestGenerate docs in your project root
swag init - 2
Run it
- Add swag annotation comments to your API handler functions and general API info to main.go
- Run swag init in the project root folder that contains main.go
- Import the generated docs/docs.go package in your application
- Serve Swagger UI using the plugin for your web framework
- 3
Get the most out of it
- Use the --tags option to include or exclude specific APIs from the docs
- Use swag fmt to format annotation comments
- Use --markdownFiles to pull long descriptions from markdown files
- Use -g to point to a different file when general API info is not in main.go
- Use the --generalInfo and --dir flags to control which files are parsed
Downloads
Strengths
- Docs live next to the code, so they are easier to keep in sync
- Plugins are listed for many popular Go web frameworks
- Can be installed with go install or run through a Docker image
Trade-offs
- Output is Swagger 2.0 only according to the README, which is older than OpenAPI 3
- Documentation quality depends on how carefully annotations are written
- Latest release shown is a release candidate (v2.0.0-rc6), so it may not be the most stable choice
How it compares
| Project | Stars | Type | Level | Updated | Best for |
|---|---|---|---|---|---|
| swag (this) | 13k | Dev tool | intermediate | 1 months ago | Go developers who already have a REST API using Gin, Echo, net/http, Fiber, or another listed framework |
| scalar | 16.4k | App | beginner | 4 days ago | Developers who already maintain an OpenAPI or Swagger document |
| redoc | 26k | Library | beginner | 5 days ago | Teams that already write OpenAPI or Swagger specs and want public or internal API docs |
| mailpit | 10.8k | Dev tool | beginner | 4 days ago | Developers testing signup, password reset, or notification emails locally |
| act | 72.5k | Dev tool | intermediate | 5 days ago | Developers who edit .github/workflows files and want faster feedback than commit-and-push cycles |
| pocketbase | 61.6k | App | beginner | 10 days ago | Developers building a small web or mobile app who want a ready-made backend |
Stats synced from GitHub. The assessment above was drafted with AI from the project's README on 2026-10-29 and may miss recent changes — check the README before relying on a command. MIT licence: free to use, modify and distribute, including commercially, as long as you keep the copyright notice.