func handleRequest(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Write([]byte("Hello, World!")) }
func handleRequest(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusCreated) json.NewEncoder(w).Encode(map[string]string{"message": "Resource created successfully"}) }This example first sets the Content-Type header to application/json and then uses the WriteHeader method to set the status code to 201. It then encodes a map into JSON format using the json.NewEncoder method and writes it to the response body. Package library: net/http, encoding/json.