HTTP Services & JSON
A REST API with only the standard library: routing, handlers, JSON encoding and struct tags.
Go's standard library includes a production-quality HTTP server and client and JSON support, which is why so many APIs and microservices are written in Go. Since Go 1.22, the built-in router understands methods and path parameters.
A small readings API
struct tags like json:"sensor" set JSON field names; omitempty skips empty values.
mux.HandleFunc("GET /readings/{sensor}", ...) routes by method and path; r.PathValue reads the parameter.
json.NewEncoder(w).Encode writes JSON; json.NewDecoder(r.Body).Decode reads it.
httptest.NewServer starts the API on a real local port for the demo; http.Get and http.Post call it.
Production notes
Using a plain map shared by all handlers without a lock Guard it with sync.RWMutex (or use a database)
The HTTP server runs handlers concurrently, so unsynchronized map writes are a data race (and Go may crash with 'concurrent map writes').
Why do Go struct fields need to be capitalized for encoding/json?
encoding/json can only access exported (capitalized) fields; struct tags then choose the JSON names.