package main import ( "log" "net/http" "github.com/gorilla/mux" ) func main() { r := mux.NewRouter() // Handle requests to URLs starting with /admin r.PathPrefix("/admin").Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("This is the admin page")) })) log.Fatal(http.ListenAndServe(":8080", r)) }
package main import ( "log" "net/http" "github.com/gorilla/mux" ) func main() { r := mux.NewRouter() // Use PathPrefix to handle requests to multiple subdomains subrouter := r.PathPrefix("/{subdomain}").Subrouter() // Handle requests to /blog on all subdomains subrouter.PathPrefix("/blog").Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("This is the blog page")) })) // Handle requests to /shop on a specific subdomain subrouter.Host("shop.example.com").PathPrefix("/shop").Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("This is the shop page")) })) log.Fatal(http.ListenAndServe(":8080", r)) }In this example, we use `PathPrefix()` and `Subrouter()` to create a subrouter that handles requests for multiple subdomains. We then use `Host()` with `PathPrefix()` to handle requests to a specific subdomain and path. The `gorilla/mux` package is part of the Gorilla web toolkit for Go, a collection of packages for building web applications.