1
0
mirror of https://github.com/tmrts/go-patterns.git synced 2024-11-25 14:36:06 +03:00

create broadcast page and link to main Readme

This commit is contained in:
Efim Zabarsky 2024-08-26 13:28:57 +03:00
parent f978e42036
commit 12855d0ea4
2 changed files with 50 additions and 1 deletions

View File

@ -63,7 +63,7 @@ A curated collection of idiomatic design & application patterns for Go language.
|:-------:|:----------- |:------:| |:-------:|:----------- |:------:|
| [N-Barrier](/concurrency/barrier.md) | Prevents a process from proceeding until all N processes reach to the barrier | ✘ | | [N-Barrier](/concurrency/barrier.md) | Prevents a process from proceeding until all N processes reach to the barrier | ✘ |
| [Bounded Parallelism](/concurrency/bounded_parallelism.md) | Completes large number of independent tasks with resource limits | ✔ | | [Bounded Parallelism](/concurrency/bounded_parallelism.md) | Completes large number of independent tasks with resource limits | ✔ |
| [Broadcast](/concurrency/broadcast.md) | Transfers a message to all recipients simultaneously | | | [Broadcast](/concurrency/broadcast.md) | Transfers a message to all recipients simultaneously | |
| [Coroutines](/concurrency/coroutine.md) | Subroutines that allow suspending and resuming execution at certain locations | ✘ | | [Coroutines](/concurrency/coroutine.md) | Subroutines that allow suspending and resuming execution at certain locations | ✘ |
| [Generators](/concurrency/generator.md) | Yields a sequence of values one at a time | ✔ | | [Generators](/concurrency/generator.md) | Yields a sequence of values one at a time | ✔ |
| [Reactor](/concurrency/reactor.md) | Demultiplexes service requests delivered concurrently to a service handler and dispatches them syncronously to the associated request handlers | ✘ | | [Reactor](/concurrency/reactor.md) | Demultiplexes service requests delivered concurrently to a service handler and dispatches them syncronously to the associated request handlers | ✘ |

49
concurrency/broadcast.md Normal file
View File

@ -0,0 +1,49 @@
# 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
```go
// 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
```go
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.
1. It uses a *sync.WaitGroup* to wait for all goroutines to finish sending the message.
1. Each recipient channel receives the message simultaneously through a goroutine.