broadcast

package module
v0.0.0-...-bdf24f9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 4, 2018 License: BSD-3-Clause Imports: 0 Imported by: 0

README

Broadcast

Broadcast channels for Go! This is a lightweight implementation using just channels and no other synchronization

How to Use:

Making a channel:

b := make(broadcast.Broadcast)

Broadcasting a message:

b <- message // Combine with whatever select magic you'd like!

Receiving a message:

message := b.Receive()

More info: https://godoc.org/github.com/ericpauley/broadcast

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:

func (Broadcast) Receive

func (b Broadcast) Receive() (msg interface{})

Receive waits for a message to be received. If more receivers are listening then Receive will forward the packet to them as well.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL