From: Ferenc Szabo Date: Thu, 25 Jun 2020 09:12:50 +0000 (+0200) Subject: doc: add note about missing lock in sample code X-Git-Tag: go1.15rc1~45 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=c4fd3f6ff60189ba18446d538523b646f37f930b;p=gostls13.git 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 --- diff --git a/doc/effective_go.html b/doc/effective_go.html index 9be6bc7cb0..7620402984 100644 --- a/doc/effective_go.html +++ b/doc/effective_go.html @@ -2336,10 +2336,9 @@ of the request from the client.

For brevity, let's ignore POSTs and assume HTTP requests are always -GETs; that simplification does not affect the way the handlers are -set up. Here's a trivial but complete implementation of a handler to -count the number of times the -page is visited. +GETs; that simplification does not affect the way the handlers are set up. +Here's a trivial implementation of a handler to count the number of times +the page is visited.

 // Simple counter server.
@@ -2355,6 +2354,11 @@ func (ctr *Counter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
 

(Keeping with our theme, note how Fprintf can print to an http.ResponseWriter.) +In a real server, access to ctr.n would need protection from +concurrent access. +See the sync and atomic packages for suggestions. +

+

For reference, here's how to attach such a server to a node on the URL tree.