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:
@@ -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")
|
||||
}
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
bfdaa0c8104c1257e6fea102fd26d476a3e8c14e
|
||||
6Y8t3Yxd1LD
|
||||
3737e7b5129b649d202e75225a1ac732eda116d0
|
||||
rovAFf9-n78
|
||||
|
||||
@@ -10,7 +10,6 @@ goroutine : 0
|
||||
going
|
||||
goroutine : 1
|
||||
goroutine : 2
|
||||
<enter>
|
||||
done
|
||||
|
||||
# Next we'll look at a complement to goroutines in
|
||||
|
||||
Reference in New Issue
Block a user