go by example part 3
This commit is contained in:
24
5-go-by-example/26-channel-synchronization.go
Normal file
24
5-go-by-example/26-channel-synchronization.go
Normal 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 function’s 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
|
||||
}
|
||||
Reference in New Issue
Block a user