Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

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.

PreviousChannel BufferingNextChannel Directions

Last updated 1 year ago

Was this helpful?