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:
parent
be9b84288c
commit
61e8dde1c1
@ -2,7 +2,10 @@
|
|||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
func f(from string) {
|
func f(from string) {
|
||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < 3; i++ {
|
||||||
@ -29,9 +32,8 @@ func main() {
|
|||||||
}("going")
|
}("going")
|
||||||
|
|
||||||
// Our two function calls are running asynchronously in
|
// Our two function calls are running asynchronously in
|
||||||
// separate goroutines now, so execution falls through
|
// separate goroutines now. Wait for them to finish
|
||||||
// to here. This `Scanln` requires we press a key
|
// (for a more robust approach, use a [WaitGroup](waitgroups)).
|
||||||
// before the program exits.
|
time.Sleep(time.Second)
|
||||||
fmt.Scanln()
|
|
||||||
fmt.Println("done")
|
fmt.Println("done")
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
bfdaa0c8104c1257e6fea102fd26d476a3e8c14e
|
3737e7b5129b649d202e75225a1ac732eda116d0
|
||||||
6Y8t3Yxd1LD
|
rovAFf9-n78
|
||||||
|
@ -10,7 +10,6 @@ goroutine : 0
|
|||||||
going
|
going
|
||||||
goroutine : 1
|
goroutine : 1
|
||||||
goroutine : 2
|
goroutine : 2
|
||||||
<enter>
|
|
||||||
done
|
done
|
||||||
|
|
||||||
# Next we'll look at a complement to goroutines in
|
# Next we'll look at a complement to goroutines in
|
||||||
|
17
public/goroutines
generated
17
public/goroutines
generated
@ -41,7 +41,7 @@
|
|||||||
|
|
||||||
</td>
|
</td>
|
||||||
<td class="code leading">
|
<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>
|
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||||
</pre></div>
|
</pre></div>
|
||||||
|
|
||||||
@ -54,7 +54,10 @@
|
|||||||
</td>
|
</td>
|
||||||
<td class="code leading">
|
<td class="code leading">
|
||||||
|
|
||||||
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"fmt"</span>
|
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
|
||||||
|
<span class="s">"fmt"</span>
|
||||||
|
<span class="s">"time"</span>
|
||||||
|
<span class="p">)</span>
|
||||||
</pre></div>
|
</pre></div>
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
@ -137,14 +140,13 @@ function call.</p>
|
|||||||
<tr>
|
<tr>
|
||||||
<td class="docs">
|
<td class="docs">
|
||||||
<p>Our two function calls are running asynchronously in
|
<p>Our two function calls are running asynchronously in
|
||||||
separate goroutines now, so execution falls through
|
separate goroutines now. Wait for them to finish
|
||||||
to here. This <code>Scanln</code> requires we press a key
|
(for a more robust approach, use a <a href="waitgroups">WaitGroup</a>).</p>
|
||||||
before the program exits.</p>
|
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
<td class="code">
|
<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">"done"</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">"done"</span><span class="p">)</span>
|
||||||
<span class="p">}</span>
|
<span class="p">}</span>
|
||||||
</pre></div>
|
</pre></div>
|
||||||
@ -174,7 +176,6 @@ goroutines being run concurrently by the Go runtime.</p>
|
|||||||
<span class="go">going</span>
|
<span class="go">going</span>
|
||||||
<span class="go">goroutine : 1</span>
|
<span class="go">goroutine : 1</span>
|
||||||
<span class="go">goroutine : 2</span>
|
<span class="go">goroutine : 2</span>
|
||||||
<span class="go"><enter></span>
|
|
||||||
<span class="go">done</span>
|
<span class="go">done</span>
|
||||||
</pre></div>
|
</pre></div>
|
||||||
|
|
||||||
@ -206,7 +207,7 @@ concurrent Go programs: channels.</p>
|
|||||||
</div>
|
</div>
|
||||||
<script>
|
<script>
|
||||||
var codeLines = [];
|
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>
|
||||||
<script src="site.js" async></script>
|
<script src="site.js" async></script>
|
||||||
</body>
|
</body>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user