Add note to waitgroups example about errgroup

Fixes #308
This commit is contained in:
Eli Bendersky 2021-08-30 09:18:16 -07:00
parent df8a378a22
commit 69efd4b8c0
3 changed files with 25 additions and 5 deletions

View File

@ -39,4 +39,9 @@ func main() {
// Block until the WaitGroup counter goes back to 0; // Block until the WaitGroup counter goes back to 0;
// all the workers notified they're done. // all the workers notified they're done.
wg.Wait() 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).
} }

View File

@ -1,2 +1,2 @@
fd77f5122e6df1669c0a2e0d2c4dfbd30631c21f b87ababcf7e1ce54107252c658840097bb6060a7
7mWXl0yVe6I vXBl8zQpDYj

21
public/waitgroups generated
View File

@ -42,7 +42,7 @@ use a <em>wait group</em>.</p>
</td> </td>
<td class="code leading"> <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 class="chroma"><span class="kn">package</span> <span class="nx">main</span>
</pre> </pre>
</td> </td>
@ -165,10 +165,25 @@ counter for each.</p>
all the workers notified they&rsquo;re done.</p> all the workers notified they&rsquo;re done.</p>
</td> </td>
<td class="code"> <td class="code leading">
<pre class="chroma"> <pre class="chroma">
<span class="nx">wg</span><span class="p">.</span><span class="nf">Wait</span><span class="p">()</span> <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> <span class="p">}</span>
</pre> </pre>
</td> </td>
@ -223,7 +238,7 @@ is likely to be different for each invocation.</p>
</div> </div>
<script> <script>
var codeLines = []; 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>
<script src="site.js" async></script> <script src="site.js" async></script>
</body> </body>