Junior — Middle
Could you provide an example of code implementation to illustrate this concept?
sobes.tech AI
Answer from AI
I'll provide an example of a simple HTTP server in Go, which demonstrates the basic handling of requests — illustrating the concept of creating a web service.
package main
import (
"fmt"
"net/http"
)
func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, world!")
}
func main() {
http.HandleFunc("/", helloHandler)
fmt.Println("Server started at http://localhost:8080")
http.ListenAndServe(":8080", nil)
}
This code creates a simple server that responds with "Hello, world!" to all HTTP requests to the root path. Such an example helps understand the basic principles of working with HTTP in Go.