diff --git a/examples/goroutines/goroutines.go b/examples/goroutines/goroutines.go index c2bdde8..4943ec9 100644 --- a/examples/goroutines/goroutines.go +++ b/examples/goroutines/goroutines.go @@ -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") } diff --git a/examples/goroutines/goroutines.hash b/examples/goroutines/goroutines.hash index e86550b..a83b7b8 100644 --- a/examples/goroutines/goroutines.hash +++ b/examples/goroutines/goroutines.hash @@ -1,2 +1,2 @@ -bfdaa0c8104c1257e6fea102fd26d476a3e8c14e -6Y8t3Yxd1LD +3737e7b5129b649d202e75225a1ac732eda116d0 +rovAFf9-n78 diff --git a/examples/goroutines/goroutines.sh b/examples/goroutines/goroutines.sh index 2dcf3bb..1d42ee2 100644 --- a/examples/goroutines/goroutines.sh +++ b/examples/goroutines/goroutines.sh @@ -10,7 +10,6 @@ goroutine : 0 going goroutine : 1 goroutine : 2 - done # Next we'll look at a complement to goroutines in diff --git a/public/goroutines b/public/goroutines index a7a4222..6b87012 100644 --- a/public/goroutines +++ b/public/goroutines @@ -41,7 +41,7 @@ - +
package main
 
@@ -54,7 +54,10 @@ -
import "fmt"
+          
import (
+    "fmt"
+    "time"
+)
 
@@ -137,14 +140,13 @@ function call.

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.

+separate goroutines now. Wait for them to finish +(for a more robust approach, use a WaitGroup).

-
    fmt.Scanln()
+          
    time.Sleep(time.Second)
     fmt.Println("done")
 }
 
@@ -174,7 +176,6 @@ goroutines being run concurrently by the Go runtime.

going goroutine : 1 goroutine : 2 -<enter> done
@@ -206,7 +207,7 @@ concurrent Go programs: channels.