In this article, we explore the powerful tool 'go-swagger', and how it can be used to generate Swagger documentation files directly from Go code. Swagger, also known as OpenAPI, is a widely-accepted standard for designing, building, and documenting REST APIs, providing interactive documentation, client SDK generation, and API discoverability.

Why do you need Swagger docs?

As a beginner software engineer, understanding and implementing efficient documentation practices is critical. Documenting your commercial code helps your colleagues better understand the design and function of your codebase, making it easier to maintain and expand upon. However, manually writing API documentation can be time-consuming and error-prone.

That's where tools like go-swagger come in. This open-source project can generate Swagger specification documentation automatically from your Go code. This means you'll be able to focus on developing your code, while also maintaining clear, accurate, and up-to-date API documentation with minimal effort.

The generation of a Swagger specification from the source code of a Go program is done by two things:

Prepare some code

We need to create a docs directory at the external level of the project, in which we create a single docs.go file inside. In fact, you can put the directory anywhere, as long as the package is called docs, and the name of the file does not matter. I will give here an example of the content of the docs.go file, so that it will be clear enough for understanding:

// Package classification Hellow Hachernoon
//
// Documentation for my go project
//
//     Schemes: http
//     BasePath: /
//     Version: 1.0.0
//     Host:
//
//     Consumes:
//     - application/json
//     - multipart/form-data
//
//     Produces:
//     - application/json
//
//     Security:
//     - basic
//
//     SecurityDefinitions:
//     basic:
//       type: basic
//
// swagger:meta
package docs

A couple of comments:

Importing docs In your main.go looks like this:

import _ "path/to/docs"

Don’t forget to import it, otherwise, you will catch the joy of the fact that empty documentation will be generated without errors.

Route wrapper

Write all the binding in the same docs package, it can be in one file, it can be in several, or even in docs.go, it doesn't matter. Perhaps, you can write wrappers right where your handlers are, but I haven’t tried it, so I won’t argue with it, but in this case, you need to start importing these wrappers in the docs package.

For example, let's create a docs/routes.go file. Let's describe a wrapper for one /payloads route in it:

// swagger:route GET /payload payloads GetPayload
// Get payload with given ID.
// responses:
//   200: GetPayloadRes200

// swagger:parameters GetPayload
type GetPayloadReq struct {
   core.GetPayloadReq
}

// swagger:response GetPayloadRes200
type GetPayloadRes200 struct {
   // in:body
   Body core.GetPayloadRes
}

Our points:

GET request parameters

This type of request is characterized by the following details:

It is important here, firstly, to write // in:query (once for the entire list of Query parameters), and secondly, to specify the query:"paramName" tag for each field. The structure of the output parameters follows the same rules as the input parameters for a POST request, since the output is always Body.

POST request parameters

This type of request is characterized by the following details:

Generate Swagger doc

cd /path/to/your/project
SWAGGER_GENERATE_EXTENSION=false swagger generate spec -o /path/to/swagger.yaml

It is important that your project has main.go file at the top level, because the go-swagger utility relies on when the generation starts.

SWAGGER_GENERATE_EXTENSION is an optional setting that removes the following default behavior: Go type descriptions in swagger model descriptions. In other words, by default, the utility saves information about what Go-types are behind each field of each model and stores this in special attributes that Swagger UI. In my opinion, it is absolutely unnecessary information for users of the Swagger specification.