import ( "net/http" "github.com/gorilla/mux" ) func main() { r := mux.NewRouter() r.HandleFunc("/home", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Welcome to my website!")) }) http.ListenAndServe(":8080", r) }
import ( "fmt" "net/http" "github.com/gorilla/mux" ) func main() { r := mux.NewRouter() r.HandleFunc("/products/{category}/{id}", func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) // Get the route variables w.WriteHeader(http.StatusOK) fmt.Fprintf(w, "Category: %v\n", vars["category"]) fmt.Fprintf(w, "ID: %v\n", vars["id"]) }) http.ListenAndServe(":8080", r) }In this example, we define a route handler for the path "/products/{category}/{id}". This route contains two parameters: "category" and "id". When a user navigates to a path that matches this route, we extract the parameter values using `mux.Vars(r)`. In summary, `github.com/gorilla/mux` is a package library for building HTTP services that provides a flexible and easy-to-use router. It allows for defining complex routes with parameters and has a number of other useful features, such as middleware support and sub-routing capabilities.