]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: add http.NotFoundHandler example
authoresell <eujon.sellers@gmail.com>
Thu, 30 Aug 2018 18:22:53 +0000 (12:22 -0600)
committerDaniel Martí <mvdan@mvdan.cc>
Thu, 27 Sep 2018 07:40:12 +0000 (07:40 +0000)
Change-Id: I6a69c7a5b829a967d75e1c79210a4906c0d8f505
Reviewed-on: https://go-review.googlesource.com/132276
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/net/http/example_test.go

index f5c47d0bd4461efaa36ea52478f840112b747ecc..2a09f5f6c6965323a5e642cf5f26f6ea07df792c 100644 (file)
@@ -173,3 +173,21 @@ func ExampleHandleFunc() {
 
        log.Fatal(http.ListenAndServe(":8080", nil))
 }
+
+func newPeopleHandler() http.Handler {
+       return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+               fmt.Fprintln(w, "This is the people handler.")
+       })
+}
+
+func ExampleNotFoundHandler() {
+       mux := http.NewServeMux()
+
+       // Create sample handler to returns 404
+       mux.Handle("/resources", http.NotFoundHandler())
+
+       // Create sample handler that returns 200
+       mux.Handle("/resources/people/", newPeopleHandler())
+
+       log.Fatal(http.ListenAndServe(":8080", mux))
+}