Update waitgroups example to use go 1.22 (#508)
gorutine closures inside for loops does not require variable reassignment since the 1.22 for loop changes
This commit is contained in:
@@ -29,10 +29,6 @@ func main() {
|
||||
// counter for each.
|
||||
for i := 1; i <= 5; i++ {
|
||||
wg.Add(1)
|
||||
// Avoid re-use of the same `i` value in each goroutine closure.
|
||||
// See [the FAQ](https://golang.org/doc/faq#closures_and_goroutines)
|
||||
// for more details.
|
||||
i := i
|
||||
|
||||
// Wrap the worker call in a closure that makes sure to tell
|
||||
// the WaitGroup that this worker is done. This way the worker
|
||||
|
@@ -1,2 +1,2 @@
|
||||
66d1c1cdb7e60f63b9b30938aa9c63b2262463ac
|
||||
S98GjeaGBX0
|
||||
c81a54fed0cd96464456e05b46163329eb7c958b
|
||||
fC_Chrkb5uA
|
||||
|
17
public/waitgroups
generated
17
public/waitgroups
generated
@@ -42,7 +42,7 @@ use a <em>wait group</em>.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="https://go.dev/play/p/S98GjeaGBX0"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="https://go.dev/play/p/fC_Chrkb5uA"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<pre class="chroma"><code><span class="line"><span class="cl"><span class="kn">package</span> <span class="nx">main</span></span></span></code></pre>
|
||||
</td>
|
||||
</tr>
|
||||
@@ -122,19 +122,6 @@ counter for each.</p>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>Avoid re-use of the same <code>i</code> value in each goroutine closure.
|
||||
See <a href="https://golang.org/doc/faq#closures_and_goroutines">the FAQ</a>
|
||||
for more details.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<pre class="chroma"><code><span class="line"><span class="cl"> <span class="nx">i</span> <span class="o">:=</span> <span class="nx">i</span></span></span></code></pre>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>Wrap the worker call in a closure that makes sure to tell
|
||||
@@ -230,7 +217,7 @@ is likely to be different for each invocation.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"sync\"\u000A \"time\"\u000A)\u000A');codeLines.push('func worker(id int) {\u000A fmt.Printf(\"Worker %d starting\\n\", id)\u000A');codeLines.push(' time.Sleep(time.Second)\u000A fmt.Printf(\"Worker %d done\\n\", id)\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var wg sync.WaitGroup\u000A');codeLines.push(' for i :\u003D 1; i \u003C\u003D 5; i++ {\u000A wg.Add(1)\u000A');codeLines.push(' i :\u003D i\u000A');codeLines.push(' go func() {\u000A defer wg.Done()\u000A worker(i)\u000A }()\u000A }\u000A');codeLines.push(' wg.Wait()\u000A');codeLines.push('}\u000A');codeLines.push('');codeLines.push('');
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"sync\"\u000A \"time\"\u000A)\u000A');codeLines.push('func worker(id int) {\u000A fmt.Printf(\"Worker %d starting\\n\", id)\u000A');codeLines.push(' time.Sleep(time.Second)\u000A fmt.Printf(\"Worker %d done\\n\", id)\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var wg sync.WaitGroup\u000A');codeLines.push(' for i :\u003D 1; i \u003C\u003D 5; i++ {\u000A wg.Add(1)\u000A');codeLines.push(' go func() {\u000A defer wg.Done()\u000A worker(i)\u000A }()\u000A }\u000A');codeLines.push(' wg.Wait()\u000A');codeLines.push('}\u000A');codeLines.push('');codeLines.push('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
Reference in New Issue
Block a user