Channel Synchronization
package main
import (
"fmt"
"time"
)
// Function representing a worker that performs some work and signals completion through a channel
func worker(done chan bool) {
fmt.Print("working...") // Print a message indicating work is being done
time.Sleep(time.Second) // Simulate work by sleeping for one second
fmt.Println("done") // Print a message indicating work is done
done <- true // Send a signal (boolean value true) through the 'done' channel
}
func main() {
// Creating a buffered channel named 'done' with a capacity of 1
done := make(chan bool, 1)
// Launching a goroutine to execute the 'worker' function with the 'done' channel
go worker(done)
// Blocking operation: Receiving a signal from the 'done' channel, indicating that the work is complete
<-done
}Output
Last updated