Skip to content

Commit 3ee9672

Browse files
esellbradfitz
authored andcommitted
net/http: add Handle example
Currently there is no example for http.Handle in the documentation. This adds an example. Change-Id: I66ee9983bea1f5237757e1ef4956eae9a056e963 Reviewed-on: https://go-review.googlesource.com/137715 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent de72c43 commit 3ee9672

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/net/http/example_handle_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package http_test
6+
7+
import (
8+
"fmt"
9+
"log"
10+
"net/http"
11+
"sync"
12+
)
13+
14+
type countHandler struct {
15+
mu sync.Mutex // guards n
16+
n int
17+
}
18+
19+
func (h *countHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
20+
h.mu.Lock()
21+
defer h.mu.Unlock()
22+
h.n++
23+
fmt.Fprintf(w, "count is %d\n", h.n)
24+
}
25+
26+
func ExampleHandle() {
27+
http.Handle("/count", new(countHandler))
28+
log.Fatal(http.ListenAndServe(":8080", nil))
29+
}

0 commit comments

Comments
 (0)