Use time.Sleep instead of fmt.Scanln to wait for goroutines

The Scanln doesn't work on the Go playground

Fixes #279
This commit is contained in:
Eli Bendersky 2019-09-20 14:55:41 -07:00
parent be9b84288c
commit 61e8dde1c1
4 changed files with 18 additions and 16 deletions

View File

@ -2,7 +2,10 @@
package main
import "fmt"
import (
"fmt"
"time"
)
func f(from string) {
for i := 0; i < 3; i++ {
@ -29,9 +32,8 @@ func main() {
}("going")
// Our two function calls are running asynchronously in
// separate goroutines now, so execution falls through
// to here. This `Scanln` requires we press a key
// before the program exits.
fmt.Scanln()
// separate goroutines now. Wait for them to finish
// (for a more robust approach, use a [WaitGroup](waitgroups)).
time.Sleep(time.Second)
fmt.Println("done")
}

View File

@ -1,2 +1,2 @@
bfdaa0c8104c1257e6fea102fd26d476a3e8c14e
6Y8t3Yxd1LD
3737e7b5129b649d202e75225a1ac732eda116d0
rovAFf9-n78

View File

@ -10,7 +10,6 @@ goroutine : 0
going
goroutine : 1
goroutine : 2
<enter>
done
# Next we'll look at a complement to goroutines in

17
public/goroutines generated
View File

@ -41,7 +41,7 @@
</td>
<td class="code leading">
<a href="http://play.golang.org/p/6Y8t3Yxd1LD"><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/rovAFf9-n78"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
</pre></div>
@ -54,7 +54,10 @@
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kn">import</span> <span class="s">&quot;fmt&quot;</span>
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
<span class="s">&quot;fmt&quot;</span>
<span class="s">&quot;time&quot;</span>
<span class="p">)</span>
</pre></div>
</td>
@ -137,14 +140,13 @@ function call.</p>
<tr>
<td class="docs">
<p>Our two function calls are running asynchronously in
separate goroutines now, so execution falls through
to here. This <code>Scanln</code> requires we press a key
before the program exits.</p>
separate goroutines now. Wait for them to finish
(for a more robust approach, use a <a href="waitgroups">WaitGroup</a>).</p>
</td>
<td class="code">
<div class="highlight"><pre> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Scanln</span><span class="p">()</span>
<div class="highlight"><pre> <span class="nx">time</span><span class="p">.</span><span class="nx">Sleep</span><span class="p">(</span><span class="nx">time</span><span class="p">.</span><span class="nx">Second</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;done&quot;</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
@ -174,7 +176,6 @@ goroutines being run concurrently by the Go runtime.</p>
<span class="go">going</span>
<span class="go">goroutine : 1</span>
<span class="go">goroutine : 2</span>
<span class="go">&lt;enter&gt;</span>
<span class="go">done</span>
</pre></div>
@ -206,7 +207,7 @@ concurrent Go programs: channels.</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func f(from string) {\u000A for i := 0; i \x3C 3; i++ {\u000A fmt.Println(from, \":\", i)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' f(\"direct\")\u000A');codeLines.push(' go f(\"goroutine\")\u000A');codeLines.push(' go func(msg string) {\u000A fmt.Println(msg)\u000A }(\"going\")\u000A');codeLines.push(' fmt.Scanln()\u000A fmt.Println(\"done\")\u000A}\u000A');codeLines.push('');codeLines.push('');
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func f(from string) {\u000A for i := 0; i \x3C 3; i++ {\u000A fmt.Println(from, \":\", i)\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' f(\"direct\")\u000A');codeLines.push(' go f(\"goroutine\")\u000A');codeLines.push(' go func(msg string) {\u000A fmt.Println(msg)\u000A }(\"going\")\u000A');codeLines.push(' time.Sleep(time.Second)\u000A fmt.Println(\"done\")\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>