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.Header().Set("Content-Type", "application/json") data := map[string]string{"message": "Hello, World!"} json.NewEncoder(w).Encode(data) }This sets the response Content-Type to application/json and sends back a JSON-encoded map containing a "message" field. In both examples, we use the Write method to send the response body back to the client. We also set the Content-Type header to specify the type of data being sent. These examples use the net/http and encoding/json packages.