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