CLASSFUNC BLOG
We Share Our Knowledge
Golang gofiber swagger auto generate step by step by swaggo
Dam Van Duong
9 Th08 2023 05:27

Follow https://github.com/swaggo/swag

Github Example: https://github.com/duongdam/go-swaggo

Step 1: Install swaggo

go install github.com/swaggo/swag/cmd/swag@latest

Step 2: Install gofiber swagger

go get -u github.com/gofiber/swagger

Step 3: Add comments to your API source code

Example your go.mod

module go_api go 1.20

Example your main.go

Notice the import -> _ "go_api/docs", and change info of // @title... if you want.

package main import ( "github.com/gofiber/swagger" "github.com/gofiber/fiber/v2" _ "go_api/docs" ) // @title GoFiber Example API // @version 1.0 // @description Golang GoFiber swagger auto generate step by step by swaggo // @termsOfService http://swagger.io/terms/ // @contact.name API Support // @contact.email fiber@swagger.io // @license.name Apache 2.0 // @license.url http://www.apache.org/licenses/LICENSE-2.0.html // @host localhost:8080 // @BasePath / func main() { app := fiber.New() app.Get("/swagger/*", swagger.HandlerDefault) // default app.Post("/user/create", userController.HandlerCreateUser) // create new user app.Listen(":8080") }

Example your function create user

package userController import (...) // HandlerCreateUser godoc // // @Summary Create new user // @Description Create new user // @Tags users // @Accept json // @Produce json // @Param payload body userModel.UserModel true "UserModel" // @Success 200 {string} string "OK" // @Failure 400 {string} error "Bad Request" // @Router /user/create [post] func HandlerCreateUser(c *fiber.Ctx) error { ... } # Notice: Remember line "// @Router /user/create [post]" and line "func HandlerCreateUser(c *fiber.Ctx) error {" do not have a newline.

-> Done step 3

Step 4: Run the Swag in your Go project root folder which contains main.go file, Swag will parse comments and generate required files(docs folder and docs/doc.go).

swag init

Step 5: Run go project and move to http://localhost:8080/swagger/index.html

go run .

Results:

Ảnh màn hình 2023-08-09 lúc 13.34.20.png

Thanks for reading