Documentation ¶
Overview ¶
Package broadcast implements a channel which broadcasts messages to all listeners simultaneously.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Broadcast ¶
type Broadcast chan interface{}
Broadcast represents a channel which can have a single item received by multiple goroutines.
In every sense but receiving it can be used like a standard chan interface{}. Sends will block until at least one goroutine has received the interface. Sends will panic if the channel has been closed.
Channels intended for broadcasting should NOT be listened to directly.
If more than one channel is listening at the time the message is sent then they will all receive the message simultaneously.
Example ¶
package main import ( "sync" "time" "github.com/ericpauley/broadcast" ) func main() { var s sync.WaitGroup b := make(broadcast.Broadcast) // Broadcast channels are constructed exactly like regular channels s.Add(10) for i := 0; i < 10; i++ { go func() { b.Receive() time.Sleep(1 * time.Millisecond) s.Done() }() } b <- 1 close(b) s.Wait() }
Output:
Click to show internal directories.
Click to hide internal directories.