Lesson 1 of 9 · 22m
Start with net/http, Not a Framework-shaped Hole
Design the small server core first: explicit construction, clean handler interfaces, configuration boundaries, and honest ownership of dependencies.
The first Go service I inherited had three routers, two JSON libraries, and a dependency-injection framework hiding the fact that nobody knew where requests entered the process. Go does not reward that theater. net/http is small, sharp, and usually enough. Start there; add dependencies when they solve a measured problem.
The handler is the center of gravity
An HTTP handler is one interface:
type Handler interface {
ServeHTTP(http.ResponseWriter, *http.Request)
}That simplicity scales. Your application can expose narrow, testable units without framework lifecycle rules:
type Server struct {
logger *slog.Logger
store OrderStore
}
func (s *Server) routes() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("GET /healthz", s.healthz)
mux.HandleFunc("GET /orders/{id}", s.getOrder)
mux.HandleFunc("POST /orders", s.createOrder)
return mux
}Go 1.22 added method and wildcard patterns to ServeMux. Use them; they are explicit enough for many services.
Construct dependencies in main
Production clarity starts with boring construction. Read config, open clients, wire the server, and return errors instead of hiding fatal exits inside library code:
srv := &http.Server{
Addr: cfg.ListenAddr,
Handler: NewServer(logger, store).routes(),
ReadHeaderTimeout: 5 * time.Second,
}main owns process lifetime. Packages own behavior. When packages call os.Exit during ordinary setup failure, operators lose control and tests become miserable.
Keep transport separate from domain logic
HTTP concerns are decoding, encoding, status codes, headers, deadlines, and auth context. Business logic should not know whether it was invoked by HTTP, a queue consumer, or a test:
order, err := s.store.Order(r.Context(), id)
if errors.Is(err, ErrNotFound) {
http.Error(w, "order not found", http.StatusNotFound)
return
}This separation looks obvious until pressure arrives. Then handlers mutate domain state directly, domain packages format responses, and every change requires understanding the whole program.
Configuration should be explicit and small
Use environment variables or strict files. Every key needs an owner and a failure mode:
HTTP_ADDR=:8080
DATABASE_URL=postgres://orders:...@127.0.0.1:5432/orders
SHUTDOWN_TIMEOUT=15s
READ_HEADER_TIMEOUT=5sDo not accept arbitrary runtime toggles unless there is an operational reason. Hidden configuration is hidden behavior.
Field notes
http.ListenAndServealone hides server construction. Constructhttp.Serverexplicitly.- Never store request-scoped state in fields on long-lived handlers; handlers are shared across goroutines.
- A router dependency is fine when you need its features. Three routers means nobody made a decision.
- Log dependency startup failures with context, but never include credentials or full connection strings.
Was this lesson helpful?