Use own router

It's tempting to just slap on more handlers for various resources using the builtin router but once the path based routing is insufficient, you'll have to replace it. Let's forego the issue by using a router of our own. I use the word router interchangeably with muxer. This image represents a muxer (borrowed from wikipedia). muxer
func main() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + router := http.NewServeMux() + router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello, World!") }) - log.Fatal(http.ListenAndServe(":8080", nil)) + log.Fatal(http.ListenAndServe(":8080", router)) }
prev toc next