func handler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/plain") w.Write([]byte("Hello, World!")) }
func handler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) fmt.Fprintf(w, "Oops! Page not Found.") }This example shows how to use ResponseWriter to return a 404 "Not Found" HTTP response with a custom message. Overall, ResponseWriter is an essential component of web applications and it provides a simple and flexible way to construct and send out HTTP responses to the client.