The net/http contract
Understand HTTP behavior beneath the framework.
The idea
Go’s standard HTTP types make request and response semantics explicit. A handler chooses headers before writing a status or body. Learn methods, status codes, routing, cancellation, and body limits even when the deployed app uses Fiber. Standard handlers and Fiber handlers have different context APIs.
Follow the flow
Make it concrete
This excerpt isolates the concept. Connect it to the booking API as you work through the exercise.
func health(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = io.WriteString(w, `{"status":"ok"}`)
}Decisions that matter
- Knowing HTTP semantics transfers across frameworks.
- Use the framework’s native request APIs deliberately.
- Set server timeouts and request-size limits, not just handler logic.
Your turn
What happens if a handler starts writing a successful body before deciding it needs to return 404?
Reveal a worked answer
The initial body write commits the response headers and default status. Resolve errors and choose status before writing the response. Test status, headers, and body together rather than checking only the JSON text.