DEV Community

Cover image for Go Channel Patterns - Wait For Result
b0r
b0r

Posted on

Go Channel Patterns - Wait For Result

To improve my Go Programming skills and become a better Go engineer, I have recently purchased an excellent on-demand education from Ardan Labs. Materials are created by an expert Go engineer, Bill Kennedy.

I have decide to record my process of learning how to write more idiomatic code, following Go best practices and design philosophies.

This series of posts will describe channel patterns used for orchestration/signaling in Go via goroutines.

Wait For Result Pattern

The main idea behind Wait For Result pattern is to have:

  • a channel that provides a signaling semantics
  • a goroutine that does some work
  • a goroutine that waits for that work to be done

Example

In this example we have an employee (a goroutine) that has some work to do. We also have a manager (main goroutine) that waits on that work to be done. Once work is done, employee notifies manager by sending a signal (paper) via communication channel ch.

Feel free to try the example on Go Playground

package main


func main() {
    // make channel of type string which provides signaling semantics
    ch := make(chan string)

    // goroutine 'a' that does some work => employee
    go func() {
        // simulate the idea of unknown latency (do not use in production)
        time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
                // when work is done send signal
        ch <- "paper"

        // we don't know the order of print statement
        fmt.Println("employee: sent signal")
    }()

    // goroutine 'main' => manager 
    // goroutines 'main' and 'a' are executed in parallel

    // wait for and receive signal from 'goroutine a'
        // blocking operation
    p := <-ch


    // we don't know which print statement is going to be executed first
    fmt.Println("manager: received signal :", p)

    // ensure enough time to get the result (do not use in production)
    time.Sleep(time.Second)
}
Enter fullscreen mode Exit fullscreen mode

Result (1st execution)

go run main.go
employee: sent signal
manager: received signal : paper
Enter fullscreen mode Exit fullscreen mode

Result (2st execution)

go run main.go
manager: received signal : paper
employee: sent signal
Enter fullscreen mode Exit fullscreen mode

Most common misconceptions:

  1. Note that in the first and the second execution of the previous code, the order of fmt.Println() statement is not guaranteed. Goroutines are executed in parallel.

Conclusion

In this article, wait for result channel pattern was described. In addition, simple implementation was provided.

Readers are encouraged to check out excellent Ardan Labs education materials to learn more.

Resources:

  1. Ardan Labs
  2. Cover image by Igor Mashkov from Pexels

Top comments (0)