Channel Synchronization

This Go code showcases the use of a channel (done) to signal the completion of work in a goroutine.

Let's break it down with inline comments:

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

working...done

Explanation:

  1. package main: Indicates that this Go file belongs to the main executable package.

  2. import (...): Imports necessary packages, including "fmt" for formatting and printing, and "time" for handling time-related operations.

  3. func worker(done chan bool) { ... }: Defines a function worker that takes a channel done as a parameter. This function simulates work by printing messages, sleeping for one second, and then sending a signal (true) through the channel to indicate that the work is complete.

  4. func main() { ... }: The main function, where the execution of the program begins.

  5. done := make(chan bool, 1): Creates a buffered channel named 'done' with a capacity of 1. The buffer size ensures that the channel can hold one value without requiring a corresponding receiver to be ready.

  6. go worker(done): Launches a new goroutine to execute the worker function with the done channel as a parameter.

  7. <-done: Blocks the main goroutine until a value is received from the done channel. This serves as a synchronization point, ensuring that the main goroutine waits until the worker goroutine has completed its work and sent the signal.

In summary, this code demonstrates how to use a channel to signal the completion of work between goroutines. The main goroutine launches a worker goroutine, and the main goroutine waits for a signal from the worker before proceeding further. This pattern is commonly used for synchronization in concurrent programming.

Last updated