Introduction
This lab aims to test your knowledge of using channels to synchronize execution across goroutines.
This tutorial is from open-source community. Access the source code
This lab aims to test your knowledge of using channels to synchronize execution across goroutines.
The problem to be solved in this lab is to create a goroutine that performs some work and notifies another goroutine when it's done using a channel.
To complete this lab, you will need to:
worker
that takes a channel of type bool
as a parameter.worker
function, perform some work and then send a value to the channel to notify that the work is done.main
function, create a channel of type bool
with a buffer size of 1.worker
function and passes the channel as a parameter.main
function until a value is received from the channel.$ go run channel-synchronization.go
working...done
## If you removed the `<- done` line from this program, the
## program would exit before the `worker` even
## started.
There is the full code below:
// We can use channels to synchronize execution
// across goroutines. Here's an example of using a
// blocking receive to wait for a goroutine to finish.
// When waiting for multiple goroutines to finish,
// you may prefer to use a [WaitGroup](waitgroups).
package main
import (
"fmt"
"time"
)
// This is the function we'll run in a goroutine. 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 that we're 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 we receive a notification from the
// worker on the channel.
<-done
}
In this lab, you learned how to use channels to synchronize execution across goroutines. By creating a channel and passing it to a goroutine, you can wait for the goroutine to finish its work and notify you when it's done.