go by example part 3

This commit is contained in:
2022-01-05 22:56:08 +01:00
parent c1b7c64da2
commit c5983176c5
10 changed files with 337 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
// synchronize execution across goroutines
package main
import (
"fmt"
"time"
)
// the done channel will be used to notify another goroutine that this functions work is done
func worker(done chan bool) {
fmt.Print("working...")
time.Sleep(time.Second)
fmt.Println("done")
// Send a value to notify "done"
done <- true
}
func main() {
// start a worker goroutine, giving it the channel to notify on
done := make(chan bool, 1)
go worker(done)
// block until receive notification from the worker on the channel
<-done
}