Dependency injection

Giving a component the things it needs through its constructor instead of letting it create or find them. In Go this is plain constructors and small interfaces: main builds the pool, the queries, the service and the handler, and passes each to the next.

Minimal example

go
queries := db.New(sqlDB)
svc := restaurant.NewService(queries)
h := restaurant.NewHandler(svc)

Learn this concept

Lesson 2.5 · sqlc: SQL first, Go generated

Used in exercises

Exercise 2.6 · Generate the restaurant and menu queries Exercise 2.10 · GET /v1/restaurants with a cursor Exercise 2.12 · GET /v1/restaurants/{id}/menu in five queries Exercise 2.24 · GET /v1/restaurants: search by name
Relatedsqlc