Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. Go Programming Bootcamp

Lesson 53 of 60 · Go

Working with OpenAPI (Swagger) – Auto‑generate Docs

Duration: 20 minutes

OpenAPI with swaggo

This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.

Install the generator:

$ go install github.com/swaggo/swag/cmd/swag@latest
$ go get -u github.com/swaggo/http-swagger

Add annotations to handlers:

// @Summary Create a Todo
// @Description Add a new todo item
// @Accept json
// @Produce json
// @Param todo body Todo true "Todo item"
// @Success 201 {object} Todo
// @Router /todos [post]
func createTodo(w http.ResponseWriter, r *http.Request) { /* … */ }

Generate docs:

$ swag init -g main.go

The command creates docs/swagger.json and docs/swagger.yaml.

Serve the UI:

http.Handle("/swagger/", httpSwagger.Handler())

Visit http://localhost:8080/swagger/index.html.

Info

OpenAPI is language‑agnostic – you can auto‑generate client SDKs for TypeScript, Java, etc.

Previous: Building a Microservice with gRPCNext: Building a Serverless Function with AWS Lambda (Go Runtime)