1
0
mirror of https://github.com/tmrts/go-patterns.git synced 2025-02-27 01:23:19 +03:00
go-patterns/concurrency/broadcast.md
2024-08-26 13:28:57 +03:00

1.4 KiB

Broadcast Pattern

In Go, you can create a broadcast concurrency pattern using goroutines and channels. This pattern allows you to send a message to multiple recipients simultaneously.

Implementation

// Broadcast sends a message to all recipients simultaneously
func Broadcast(message string, recipients ...chan string) {
    var wg sync.WaitGroup
    for _, recipient := range recipients {
        wg.Add(1)
        go func(recipient chan string) {
            defer wg.Done()
            recipient <- message
        }(recipient)
    }
    wg.Wait()
}

Usage

func main() {
    // Create channels for recipients
    recipient1 := make(chan string)
    recipient2 := make(chan string)
    recipient3 := make(chan string)

    // Start goroutines to receive messages
    go func() {
        fmt.Println("Recipient 1 received:", <-recipient1)
    }()
    go func() {
        fmt.Println("Recipient 2 received:", <-recipient2)
    }()
    go func() {
        fmt.Println("Recipient 3 received:", <-recipient3)
    }()

    // Broadcast a message to all recipients
    Broadcast("Hello, World!", recipient1, recipient2, recipient3)
}
  1. The Broadcast function takes a message and a variable number of recipient channels.
  2. It uses a sync.WaitGroup to wait for all goroutines to finish sending the message.
  3. Each recipient channel receives the message simultaneously through a goroutine.