basic

command
v14.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Controllers: a constructor and a Routing method, mounted with app.Router.

A controller is a plain struct with exactly two things: NewXxxAPI, the constructor that declares its dependencies, and Routing(r iris.Router), the route table the router calls once at startup. Mounting resolves the constructor through the container: app.Router("/notes", NewNotesAPI) on any router, or the builder's API("/notes", NewNotesAPI) step on the production stack; the same constructor either way.

Handlers are methods, so they share the struct's dependencies instead of reaching for a package-level variable, and the route table stays in one readable place.

Learn: write a controller as a NewXxxAPI constructor plus a Routing method, register its dependency once and mount it with app.Router.

Run:

go run main.go

Try:

curl http://localhost:8080/notes                                # 200, one seeded note
curl -X POST http://localhost:8080/notes -d '{"title":"buy milk"}'  # 201, {"id":2}
curl -X POST http://localhost:8080/notes -d '{}'                # 400, title is required
curl http://localhost:8080/notes/1                              # 200, the note
curl -i -X DELETE http://localhost:8080/notes/1                 # 204
curl -i http://localhost:8080/notes/404                         # 404, from errors.NotFound

Next: ../nested-guards

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL