r := mux.NewRouter() r.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello World!") })
func main() { r := mux.NewRouter() r.HandleFunc("/greet/{name}", greetHandler) http.ListenAndServe(":8080", r) } func greetHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) name := vars["name"] fmt.Fprintf(w, "Hello %s!", name) }In this example, we define a new handler function `greetHandler` that takes a path variable named "name". We use the `mux.Vars()` function to extract the value of the "name" parameter from the URL path. The handler then writes a greeting message to the response writer with the name parameter included. Both examples demonstrate the use and flexibility of the `github.com/gorilla/mux` library.