Exemplo n.º 1
0
func main() {
	ps, _ := pubsub.New(16, 4)

	pch := make(fooChan)

	if err := ps.AddPublisher(pch); err != nil {
		panic(err)
	}

	wg := &sync.WaitGroup{}
	wg.Add(4)

	for _, id := range []string{"A", "B", "C", "D"} {
		ch := make(fooChan, 4)
		ps.AddSubscriber(ch)

		go func(ch <-chan foo, id string) {
			defer wg.Done()

			for v := range ch {
				fmt.Printf("%s got foo.N=%d\n", id, v.N)
			}
		}(ch, id)
	}

	for i := 0; i <= 25; i++ {
		pch <- foo{N: i}
	}
	ps.Close()

	wg.Wait()
}
Exemplo n.º 2
0
func main() {
	ps, _ := pubsub.New(16, 4)

	subs := []string{"A", "B", "C", "D"}
	for i := range subs {
		id := subs[i]
		fn := func(v interface{}) {
			fmt.Printf("%s got %d\n", id, v)
		}
		ps.SubFunc(fn)
	}

	for i := 0; i <= 25; i++ {
		ps.Pub(i)
	}
	ps.Close()
}
Exemplo n.º 3
0
func main() {
	ps, _ := pubsub.New(16, 4)

	subs := []string{"A", "B", "C", "D"}
	for i := range subs {
		id := subs[i]
		fn := fooFunc(func(v foo) {
			fmt.Printf("%s got foo.N=%d\n", id, v.N)
		})
		ps.AddSubscriber(fn)
	}

	for i := 0; i <= 25; i++ {
		ps.Pub(foo{N: i})
	}
	ps.Close()
}