How do you define a simple HTTP handler to respond with "Hello, World!" in Go?

  • func HelloWorld(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello, World!")) }
  • func HandleHelloWorld(w http.ResponseWriter, r *http.Request) { return "Hello, World!" }
  • func Hello(w http.ResponseWriter, r *http.Request) { fmt.Println("Hello, World!") }
  • func RespondHelloWorld(w http.ResponseWriter, r *http.Request) { return "Hello, World!" }
To define a simple HTTP handler that responds with "Hello, World!" in Go, you can create a function with the signature func(w http.ResponseWriter, r *http.Request). Within the function, you use the Write method of the http.ResponseWriter to send the "Hello, World!" message as the response body. This function can then be registered as a handler for a specific route in your web application.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *