Documentation ¶
Overview ¶
channel is a simple communication hub for go routines
Index ¶
- type Communication
- func (hub *Communication) AddReceiver() chan interface{}
- func (hub *Communication) AddTransmitter() chan<- interface{}
- func (hub *Communication) Close()
- func (hub *Communication) CloseAll()
- func (hub *Communication) CloseReceiver(ch chan interface{}) int
- func (hub *Communication) CloseTransmitter(ch chan<- interface{})
- func (hub *Communication) CountReceiver() int
- func (hub *Communication) Send(message interface{})
- func (hub *Communication) SendTo(message interface{}, receiver chan interface{})
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Communication ¶
type Communication struct {
// contains filtered or unexported fields
}
func Init ¶
func Init() *Communication
Init creates a new Communication
Example ¶
package main import ( "fmt" "sync" "simonwaldherr.de/go/golibs/channel" ) func main() { var w sync.WaitGroup var receiver = make(map[int]chan interface{}) w.Add(5) con := channel.Init() for i := 0; i < 5; i++ { receiver[i] = con.AddReceiver() go func(j int) { fmt.Println(<-receiver[j]) w.Done() }(i) } transmitter01 := con.AddTransmitter() transmitter01 <- "Hello World" w.Wait() for i := 0; i < 5; i++ { con.CloseReceiver(receiver[i]) } }
Output: Hello World Hello World Hello World Hello World Hello World
func (*Communication) AddReceiver ¶
func (hub *Communication) AddReceiver() chan interface{}
AddReceiver adds a new receiver to the hub
func (*Communication) AddTransmitter ¶
func (hub *Communication) AddTransmitter() chan<- interface{}
AddTransmitter adds a new transmitter to the hub
func (*Communication) Close ¶ added in v0.15.0
func (hub *Communication) Close()
Close closes the hub
func (*Communication) CloseAll ¶ added in v0.15.0
func (hub *Communication) CloseAll()
CloseAll closes all receivers and transmitters
func (*Communication) CloseReceiver ¶
func (hub *Communication) CloseReceiver(ch chan interface{}) int
CloseReceiver closes a receiver
func (*Communication) CloseTransmitter ¶ added in v0.15.0
func (hub *Communication) CloseTransmitter(ch chan<- interface{})
CloseTransmitter closes a transmitter
func (*Communication) CountReceiver ¶
func (hub *Communication) CountReceiver() int
CountReceiver returns the number of receivers
func (*Communication) Send ¶ added in v0.15.0
func (hub *Communication) Send(message interface{})
Send sends a message to all receivers
func (*Communication) SendTo ¶ added in v0.15.0
func (hub *Communication) SendTo(message interface{}, receiver chan interface{})
SendTo sends a message to a specific receiver
Click to show internal directories.
Click to hide internal directories.