diff options
author | Rob Pike <r@golang.org> | 2012-09-22 05:55:04 +1000 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2012-09-22 05:55:04 +1000 |
commit | 0809931b2f3fe52338af686188f9e4c5cdede96b (patch) | |
tree | d17899416437c96c60fc574e0e3f09dc1866da1a | |
parent | 3e9c29d50f31202aa1fa57bd968fdbda732604ba (diff) | |
download | go-0809931b2f3fe52338af686188f9e4c5cdede96b.tar.gz go-0809931b2f3fe52338af686188f9e4c5cdede96b.zip |
[release-branch.go1] faq: another way to solve the closure/variable/range complaint
««« backport 3bca7b333e00
faq: another way to solve the closure/variable/range complaint
It's easier just to declare a new variable.
R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/6501103
»»»
-rw-r--r-- | doc/go_faq.html | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/doc/go_faq.html b/doc/go_faq.html index 8264e1940a..ea6edc37e9 100644 --- a/doc/go_faq.html +++ b/doc/go_faq.html @@ -1220,8 +1220,9 @@ but <code>v</code> may have been modified since the goroutine was launched. </p> <p> -To bind the value of <code>v</code> to each closure as they are launched, one -could modify the inner loop to read: +To bind the current value of <code>v</code> to each closure as it is launched, one +must modify the inner loop to create a new variable each iteration. +One way is to pass the variable as an argument to the closure: </p> <pre> @@ -1239,6 +1240,21 @@ anonymous function. That value is then accessible inside the function as the variable <code>u</code>. </p> +<p> +Even easier is just to create a new variable, using a declaration style that may +seem odd but works fine in Go: +</p> + +<pre> + for _, v := range values { + <b>v := v</b> // create a new 'v'. + go func() { + fmt.Println(<b>v</b>) + done <- true + }() + } +</pre> + <h2 id="Control_flow">Control flow</h2> <h3 id="Does_Go_have_a_ternary_form"> |