standard

Go Standard Library

Go standard library patterns

Details

Language / Topic
goGo
Category
framework
Compatible Frameworks
standard

Rules

balanced
- Use `http.NewServeMux()` (Go 1.22+) with method-based routing: `mux.HandleFunc("GET /users/{id}", handler)`.
- Always pass `context.Context` as the first parameter — use `r.Context()` in handlers and propagate through the call chain.
- Use `http.HandlerFunc` for simple handlers and `http.Handler` interface for stateful handlers with injected dependencies.
- Use `encoding/json.NewDecoder(r.Body)` for request parsing and `json.NewEncoder(w).Encode()` for responses — set `Content-Type` header first.
- Use `context.Context` for request-scoped data, cancellation, and timeouts across API boundaries.
- Use `io.Reader` and `io.Writer` interfaces for composable I/O operations.
- Use `sync` package primitives (`Mutex`, `WaitGroup`, `Once`) for concurrency control.
- Structure packages by domain. Expose small, stable interfaces.