func main() { r := mux.NewRouter() r.HandleFunc("/", HomeHandler) http.Handle("/", r) http.ListenAndServe(":8080", nil) }
func main() { r := mux.NewRouter() r.HandleFunc("/users/{id}", UserHandler) http.Handle("/", r) http.ListenAndServe(":8080", nil) }
func main() { r := mux.NewRouter() s := r.PathPrefix("/api").Subrouter() s.HandleFunc("/users/{id}", UserHandler) http.Handle("/", r) http.ListenAndServe(":8080", nil) }In this example, a subrouter is created for the /api path prefix. The UserHandler function is added as a route for /api/users/{id}. Any requests to /api/users/{id} will be handled by this route. Overall, gorilla/mux is a very flexible package that provides powerful routing capabilities for Go web servers.