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.
Loading...
Related Quiz
- Imagine you are building a Go application to handle configurations. How would you use a map to store and manage configuration settings?
- Describe how you would write data to a file, ensuring that the file is properly closed afterward.
- Explain how indexing works in a database and why it is important.
- What are prepared statements in SQL and why are they important?
- How do you declare and initialize a variable in Go?