404
In go's standard server the root patter "/" acts as a catch-all because it ends in "/". If a muxpath ends in "/" it will match everything below it. If it ends without a "/" it will only match it's exact URL.
The best way to handle the case of wanting a 404 page is to add a check in the home handler for r.URL.Path != "/".
servemux pattern matching
In Go's servermux, longer URL patterns always take precedence over shorter ones. As a result you can register the patterns in any order and it will not change how the servermux behaves.
sanitization
Request URLs are automatically sanitized. If the request path contains any .
or ..
elements or repeated slashes, the user will be automatically redirected
to an equivalent clean URL (via a 301 Permanent Redirect).
subtree paths
If a subtree path has been registered and a request is recieved for that subtree
without a trailing slash, then the user will automatically be redirected to
the subtree path. So if the user enters /foo
and you have /foo/
registered
they will be sent there.
hostname matching
It's possible to include host names in your URL patterns. This can be useful when you want to redirect all HTTP requests to a canonical URL, or if your servermux is serving multiple sites:
mux := http.NewServeMux()
mux.HandleFunc("foo.example.org/", fooHandler)
mux.HandleFunc("bar.example.org/", barHandler)
mux.HandleFunc("/baz", bazHandler)