github.com/alphadose/zenq/v2@v2.8.4/examples/selector/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 6 "github.com/alphadose/zenq/v2" 7 ) 8 9 type custom1 struct { 10 alpha int 11 beta string 12 } 13 14 type custom2 struct { 15 gamma int 16 } 17 18 const size = 100 19 20 var ( 21 zq1 = zenq.New[int](size) 22 zq2 = zenq.New[string](size) 23 zq3 = zenq.New[custom1](size) 24 zq4 = zenq.New[*custom2](size) 25 ) 26 27 func main() { 28 go looper(intProducer) 29 go looper(stringProducer) 30 go looper(custom1Producer) 31 go looper(custom2Producer) 32 33 for i := 0; i < 40; i++ { 34 35 // Selection occurs here 36 if data := zenq.Select(zq1, zq2, zq3, zq4); data != nil { 37 switch data.(type) { 38 case int: 39 fmt.Printf("Received int %d\n", data) 40 case string: 41 fmt.Printf("Received string %s\n", data) 42 case custom1: 43 fmt.Printf("Received custom data type number 1 %#v\n", data) 44 case *custom2: 45 fmt.Printf("Received pointer %#v\n", data) 46 } 47 } 48 } 49 } 50 51 func intProducer(ctr int) { zq1.Write(ctr) } 52 53 func stringProducer(ctr int) { zq2.Write(fmt.Sprint(ctr * 10)) } 54 55 func custom1Producer(ctr int) { zq3.Write(custom1{alpha: ctr, beta: fmt.Sprint(ctr)}) } 56 57 func custom2Producer(ctr int) { zq4.Write(&custom2{gamma: 1 << ctr}) } 58 59 func looper(producer func(ctr int)) { 60 for i := 0; i < 10; i++ { 61 producer(i) 62 } 63 }