parent
df8a378a22
commit
69efd4b8c0
@ -39,4 +39,9 @@ func main() {
|
||||
// Block until the WaitGroup counter goes back to 0;
|
||||
// all the workers notified they're done.
|
||||
wg.Wait()
|
||||
|
||||
// Note that this approach has no straightforward way
|
||||
// to propagate errors from workers. For more
|
||||
// advanced use cases, consider using the
|
||||
// [errgroup package](https://pkg.go.dev/golang.org/x/sync/errgroup).
|
||||
}
|
||||
|
@ -1,2 +1,2 @@
|
||||
fd77f5122e6df1669c0a2e0d2c4dfbd30631c21f
|
||||
7mWXl0yVe6I
|
||||
b87ababcf7e1ce54107252c658840097bb6060a7
|
||||
vXBl8zQpDYj
|
||||
|
21
public/waitgroups
generated
21
public/waitgroups
generated
@ -42,7 +42,7 @@ use a <em>wait group</em>.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/7mWXl0yVe6I"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/vXBl8zQpDYj"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<pre class="chroma"><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre>
|
||||
</td>
|
||||
@ -165,10 +165,25 @@ counter for each.</p>
|
||||
all the workers notified they’re done.</p>
|
||||
|
||||
</td>
|
||||
<td class="code">
|
||||
<td class="code leading">
|
||||
|
||||
<pre class="chroma">
|
||||
<span class="nx">wg</span><span class="p">.</span><span class="nf">Wait</span><span class="p">()</span>
|
||||
</pre>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>Note that this approach has no straightforward way
|
||||
to propagate errors from workers. For more
|
||||
advanced use cases, consider using the
|
||||
<a href="https://pkg.go.dev/golang.org/x/sync/errgroup">errgroup package</a>.</p>
|
||||
|
||||
</td>
|
||||
<td class="code">
|
||||
|
||||
<pre class="chroma">
|
||||
<span class="p">}</span>
|
||||
</pre>
|
||||
</td>
|
||||
@ -223,7 +238,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, wg *sync.WaitGroup) {\u000A');codeLines.push(' defer wg.Done()\u000A');codeLines.push(' 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 go worker(i, \u0026wg)\u000A }\u000A');codeLines.push(' wg.Wait()\u000A}\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, wg *sync.WaitGroup) {\u000A');codeLines.push(' defer wg.Done()\u000A');codeLines.push(' 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 go worker(i, \u0026wg)\u000A }\u000A');codeLines.push(' wg.Wait()\u000A');codeLines.push('}\u000A');codeLines.push('');codeLines.push('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
Loading…
x
Reference in New Issue
Block a user