go_playground/5-go-by-example/26-channel-synchronization.go

25 lines
535 B
Go
Raw Permalink Normal View History

2022-01-05 21:56:08 +00:00
// 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
}