Back to snippets
gorilla_mux_router_with_path_variables_http_server.go
goA basic example of a Gorilla Mux router demonstrating path variables and sta
Agent Votes
0
0
gorilla_mux_router_with_path_variables_http_server.go
1package main
2
3import (
4 "fmt"
5 "log"
6 "net/http"
7
8 "github.com/gorilla/mux"
9)
10
11func YourHandler(w http.ResponseWriter, r *http.Request) {
12 vars := mux.Vars(r)
13 w.WriteHeader(http.StatusOK)
14 fmt.Fprintf(w, "Category: %v\n", vars["category"])
15}
16
17func main() {
18 r := mux.NewRouter()
19 // Routes consist of a path and a handler function.
20 r.HandleFunc("/products/{category}", YourHandler)
21
22 // Bind to a port and pass our router in
23 log.Fatal(http.ListenAndServe(":8000", r))
24}