mirror of
https://github.com/golang/go
synced 2024-11-25 19:47:58 -07:00
doc: add note about missing lock in sample code
The sample code in 'Interfaces and methods' section contains a data race. Handlers are served concurrently. The handler does write and read operations; `go test -race` would fail (with concurrent requests). Since the doc is frozen and the code remains less cluttered without locks/atomic, don't change the sample code. Change-Id: I654b324d2f0b7f48497822751907c7d39e2f0e3d Reviewed-on: https://go-review.googlesource.com/c/go/+/239877 Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
parent
8c521739f2
commit
c4fd3f6ff6
@ -2336,10 +2336,9 @@ of the request from the client.
|
|||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
For brevity, let's ignore POSTs and assume HTTP requests are always
|
For brevity, let's ignore POSTs and assume HTTP requests are always
|
||||||
GETs; that simplification does not affect the way the handlers are
|
GETs; that simplification does not affect the way the handlers are set up.
|
||||||
set up. Here's a trivial but complete implementation of a handler to
|
Here's a trivial implementation of a handler to count the number of times
|
||||||
count the number of times the
|
the page is visited.
|
||||||
page is visited.
|
|
||||||
</p>
|
</p>
|
||||||
<pre>
|
<pre>
|
||||||
// Simple counter server.
|
// Simple counter server.
|
||||||
@ -2355,6 +2354,11 @@ func (ctr *Counter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||||||
<p>
|
<p>
|
||||||
(Keeping with our theme, note how <code>Fprintf</code> can print to an
|
(Keeping with our theme, note how <code>Fprintf</code> can print to an
|
||||||
<code>http.ResponseWriter</code>.)
|
<code>http.ResponseWriter</code>.)
|
||||||
|
In a real server, access to <code>ctr.n</code> would need protection from
|
||||||
|
concurrent access.
|
||||||
|
See the <code>sync</code> and <code>atomic</code> packages for suggestions.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
For reference, here's how to attach such a server to a node on the URL tree.
|
For reference, here's how to attach such a server to a node on the URL tree.
|
||||||
</p>
|
</p>
|
||||||
<pre>
|
<pre>
|
||||||
|
Loading…
Reference in New Issue
Block a user