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

25 lines
535 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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
}