package main import ( "fmt" "net/http" ) func helloWorld(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello, World!") } func main() { mux := http.NewServeMux() mux.HandleFunc("/", helloWorld) http.ListenAndServe(":8000", mux) }
package main import ( "fmt" "net/http" ) func helloWorld(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello, World!") } func helloName(w http.ResponseWriter, r *http.Request) { name := r.URL.Query().Get("name") fmt.Fprintf(w, "Hello, %s!", name) } func main() { mux := http.NewServeMux() mux.HandleFunc("/", helloWorld) mux.HandleFunc("/hello", helloName) http.ListenAndServe(":8000", mux) }In this example, the first handler function `helloWorld` responds with a generic greeting for the root path "/", and the second handler function `helloName` responds with a personalized greeting using query parameters for the "/hello" path. Overall, the `net/http` package library in Go provides a powerful set of tools for creating HTTP servers and routing requests using the ServeMux struct and HandleFunc method.