package main import ( "fmt" "net/http" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello world!") }) http.ListenAndServe(":8080", nil) }
package main import ( "fmt" "net/http" ) func main() { handler := http.NewServeMux() handler.HandleFunc("/greet", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, %s!", r.FormValue("name")) }) http.ListenAndServe(":8080", handler) }This example creates a more complex HTTP server with a custom handler that handles requests to "/greet" and returns a personalized greeting based on the "name" query parameter. The handler is created using NewServeMux and is passed to ListenAndServe as the second parameter.