DEV Community

Cover image for S1E2: Concurrency Design Patterns: Boring Desing Pattern in Go
CodePiper
CodePiper

Posted on

S1E2: Concurrency Design Patterns: Boring Desing Pattern in Go

Today we will continue our Concurrency Design Pattern web series, in this blog we will learn about the Boring Design Pattern. If you want to keep update with the latest video, then please subscribe my YouTube channel (It motivate me to make more such videos for the community).
Channel Link: https://www.youtube.com/@codepiper
Boring pattern Video Link: https://www.youtube.com/watch?v=RidHWJCveLw


Let's first see where we can apply this pattern and what can be the use cases of this pattern:

  1. Small and Independent Tasks: The boring design pattern is well-suited for scenarios where you have several small and independent tasks that can be executed concurrently. Each task can be encapsulated within a separate goroutine.
  2. I/O-Bound Operations: For applications that involve I/O-bound operations (e.g., reading and writing to files, making network requests), the boring design pattern can be beneficial. Concurrently executing I/O-bound tasks using goroutines and channels can improve overall throughput and performance.
  3. Event Handling: When dealing with events or event-driven programming, the boring design pattern can be employed to handle multiple events concurrently. Each event handler can run in its own goroutine, responding to events as they occur.

Now we will see how can we implement this pattern with the help of goroutine and waitgroups

package main

import (
    "fmt"
    "sync"
)

// Boring function that returns a channel to send boring messages
func boring(msg string) <-chan string {
    ch := make(chan string)
    go func() {
        defer close(ch) // Close the channel when the goroutine exits
        for i := 1; i <= 5; i++ {
            ch <- fmt.Sprintf("%s: %d", msg, i)
        }
    }()
    return ch
}

func main() {
    aliceCh := boring("Alice")
    bobCh := boring("Bob")

    // Receive messages concurrently
    var wg sync.WaitGroup
    wg.Add(2)

    go func() {
        for msg := range aliceCh {
            fmt.Println(msg)
        }
        wg.Done()
    }()

    go func() {
        for msg := range bobCh {
            fmt.Println(msg)
        }
        wg.Done()
    }()

    wg.Wait()
}
Enter fullscreen mode Exit fullscreen mode

Github Link for the same code: https://github.com/arshad404/CodePiper/tree/main/GO/concurrency-patterns/Boring

Top comments (0)