Skip to main content

Go Swagger Generator

# Instala Go Swagger Generator v2
go get -u github.com/ruiborda/go-swagger-generator/v2
// UserDto represents the data transfer object for a user.
type UserDto struct {
ID int `json:"id" yaml:"id"`
Name string `json:"name" yaml:"name"`
}

// GetUserByIdHandler is the Gin handler for retrieving a user.
func GetUserByIdHandler(c *gin.Context) {
idStr := c.Param("id")
c.JSON(http.StatusOK, UserDto{
ID: 1, // Example ID
Name: "John Doe (User " + idStr + ")",
})
}

// --- In your ConfigureOpenAPI function (see Quick Start for full example) ---
// doc := swagger.Swagger() // Get OpenAPI builder instance
// _, _ = doc.SchemaFromDTO(&UserDto{}) // Register DTO

// Document the /users/{id} GET endpoint (relative to server URL)
var _ = doc.Path("/users/{id}").
Get(func(op openapi.Operation) {
op.Summary("Find user by ID").
Tag("User Management").
OperationID("getUserById").
PathParameter("id", func(p openapi.Parameter) {
p.Description("ID of the user to retrieve").
Required(true).
Schema(func(s openapi.Schema) {
s.Type("integer").Format("int64")
})
}).
Response(http.StatusOK, func(r openapi.Response) {
r.Description("Successful operation - user details returned").
Content(mime.ApplicationJSON, func(mt openapi.MediaType) {
mt.SchemaFromDTO(&UserDto{})
})
}).
Response(http.StatusNotFound, func(r openapi.Response) {
r.Description("User not found")
})
}).
Doc()

Generate OpenAPI/Swagger documentation for your Go APIs

Easy to Use

Easy to Integrate

Quickly integrate Go Swagger Generator into new or existing Gin projects. Complete OpenAPI 3.0 documentation with just a few lines of code.

Focus on What Matters

Fluent and Expressive API

An elegant and easy-to-use Go API that allows you to document your endpoints clearly with a fluent, chainable syntax for OpenAPI 3.0.

Powered by React

OpenAPI 3.0 Compatible

Generate documentation compatible with the OpenAPI 3.0 standard that can be visualized with Swagger UI integrated directly into your application.

Generate OpenAPI 3.0 Documentation with Go

Go Swagger Generator v2 lets you document your Go APIs with OpenAPI 3.0 in minutes

// Document the /users/{id} GET endpoint.
// The path "/users/{id}" is relative to the server URL (e.g., http://localhost:8080/v1/users/{id}).
// Assumes 'doc' is your swagger.Swagger() instance and UserDto is defined.
var _ = doc.Path("/users/{id}").
Get(func(op openapi.Operation) {
op.Summary("Find user by ID").
Tag("User Management").
OperationID("getUserById").
PathParameter("id", func(p openapi.Parameter) {
p.Description("ID of the user to retrieve").
Required(true).
Schema(func(s openapi.Schema) {
s.Type("integer").Format("int64")
})
}).
Response(http.StatusOK, func(r openapi.Response) {
r.Description("Successful operation - user details returned").
Content(mime.ApplicationJSON, func(mt openapi.MediaType) {
mt.SchemaFromDTO(&UserDto{}) // Assumes UserDto is defined
})
}).
Response(http.StatusNotFound, func(r openapi.Response) {
r.Description("User not found")
})
}).
Doc()