constructors

command
v14.0.1 Latest Latest
Warning

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

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

Documentation

Overview

Constructor injection: a value, constructor functions and an interface parameter matched by method set.

Services(store, NewUserService) registers the *MemUsers value under its concrete type, then calls NewUserService immediately - its UserRepository parameter is an interface, so the container picks the registration whose method set satisfies it. API("/users", NewUsersAPI) resolves the controller the same way, with the *UserService built a moment earlier. Registration is eager and ordered: list a dependency before the constructors that consume it.

Learn: register values and constructors with Services and let the container resolve interface parameters by method set.

Run:

go run main.go

Try:

curl http://localhost:8080/users          # 200, two users
curl http://localhost:8080/users/1        # 200, Ada
curl -i http://localhost:8080/users/404   # 404 NOT_FOUND from errors.NotFound

Next: ../handler-func

Jump to

Keyboard shortcuts

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