Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // You can edit this code!
- // Click here and start typing.
- package main
- import (
- "container/list"
- "fmt"
- "time"
- )
- type Scatter struct {
- join chan chan interface{}
- send chan interface{}
- joined *list.List
- }
- func NewScatter() *Scatter {
- scatter := &Scatter{}
- scatter.join = make(chan chan interface{})
- scatter.send = make(chan interface{})
- scatter.joined = list.New()
- go scatter.run()
- return scatter
- }
- func (s *Scatter) run() {
- for {
- select {
- case newR := <-s.join:
- s.joined.PushBack(newR)
- case newV := <-s.send:
- for el := s.joined.Front(); el != nil; el = el.Next() {
- client, ok := el.Value.(chan interface{})
- if ok {
- client <- newV
- }
- }
- }
- }
- }
- func (s *Scatter) Join(receiver chan interface{}) {
- s.join <- receiver
- }
- func (s *Scatter) Send(value interface{}) {
- s.send <- value
- }
- func main() {
- scatter := NewScatter()
- ch1 := make(chan interface{})
- ch2 := make(chan interface{})
- ch3 := make(chan interface{})
- scatter.Join(ch1)
- scatter.Join(ch2)
- scatter.Join(ch3)
- go func() {
- for {
- select {
- case val := <-ch1:
- fmt.Println("ch1: ", val)
- case val := <-ch2:
- fmt.Println("ch2: ", val)
- case val := <-ch3:
- fmt.Println("ch3: ", val)
- }
- }
- }()
- scatter.Send(10)
- scatter.Send(20)
- scatter.Send(30)
- time.Sleep(100 * time.Millisecond)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement