Canonical guidance

Use when

Avoid

Preferred pattern

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if err := s.handle(r.Context(), w, r); err != nil {
		http.Error(w, "internal error", http.StatusInternalServerError)
	}
}

Anti-pattern

Explanation: This anti-pattern is tempting when handlers need to keep work running, but detached goroutines often retain request-owned resources after the caller is gone.

Why

Related pages

Sources