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