Why this matters
Keeps handlers small, consistent, and easier to test.
For HTTP/RPC handlers, parse+validate inputs in dedicated functions. Return typed errors and reuse across endpoints.
Keeps handlers small, consistent, and easier to test.
Side-by-side examples engineers can pattern-match during review.
func createUser(w http.ResponseWriter, r *http.Request){
name := r.FormValue("name")
if name == "" { http.Error(w, "missing name", 400); return }
// repeated in other handlers
}func parseCreateUser(r *http.Request) (CreateUser, error) {
name := r.FormValue("name")
if name == "" { return CreateUser{}, ErrMissingName }
return CreateUser{Name: name}, nil
}
func createUser(w http.ResponseWriter, r *http.Request){
req, err := parseCreateUser(r)
if err != nil { http.Error(w, err.Error(), http.StatusBadRequest); return }
// use req
}r.FormValue(...) checks repeated in each handlerreq, err := parseCreateUser(r)From the same buckets as this rule.