]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: document ServeMux handling of pattern "/"
authorBrad Fitzpatrick <bradfitz@golang.org>
Thu, 12 Sep 2013 10:20:16 +0000 (11:20 +0100)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 12 Sep 2013 10:20:16 +0000 (11:20 +0100)
Fixes #4799

R=golang-dev, dave, rsc
CC=golang-dev
https://golang.org/cl/13457047

src/pkg/net/http/example_test.go
src/pkg/net/http/server.go

index bc60df7f2b587a2c73bfac87ce62befa0c7d037d..88b97d9e3d7f4e09c0f09c762771f2af11e08736 100644 (file)
@@ -68,3 +68,21 @@ func ExampleStripPrefix() {
        // URL's path before the FileServer sees it:
        http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))
 }
+
+type apiHandler struct{}
+
+func (apiHandler) ServeHTTP(http.ResponseWriter, *http.Request) {}
+
+func ExampleServeMux_Handle() {
+       mux := http.NewServeMux()
+       mux.Handle("/api/", apiHandler{})
+       mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
+               // The "/" pattern matches everything, so we need to check
+               // that we're at the root here.
+               if req.URL.Path != "/" {
+                       http.NotFound(w, req)
+                       return
+               }
+               fmt.Fprintf(w, "Welcome to the home page!")
+       })
+}
index 0c1a1408678b3b8a1d9088f6dcfcbd26e04cd7c8..67f175fd6e6487ba3167a73c64957f2ae4948bdc 100644 (file)
@@ -1358,6 +1358,10 @@ func RedirectHandler(url string, code int) Handler {
 // former will receive requests for any other paths in the
 // "/images/" subtree.
 //
+// Note that since a pattern ending in a slash names a rooted subtree,
+// the pattern "/" matches all paths not matched by other registered
+// patterns, not just the URL with Path == "/".
+//
 // Patterns may optionally begin with a host name, restricting matches to
 // URLs on that host only.  Host-specific patterns take precedence over
 // general patterns, so that a handler might register for the two patterns