func handler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) fmt.Fprintf(w, "Hello, world!") }
func handler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) fmt.Fprintf(w, "404 Page Not Found") }This example writes a 404 Not Found status code to the HTTP response using the WriteHeader method. It then writes "404 Page Not Found" to the response body using the Fprintf method. This code example belongs to the `http` package. In both examples, the WriteHeader method is used to send the HTTP status code for the response. It must be called before any data is written to the response body.