import ("fmt""math/rand""time")func fanIn(input1, input2 <-chan string) <-chan string {c := make(chan string)go func(){ for { c <- <-input1 } }()go func(){ for { c <- <-input2 } }()return c}func main() {c := fanIn(boring("foo"), boring("bar"))for i := 0; i < 10; i++ {fmt.Printf("%s\n", <-c)}fmt.Println("Finished")}func boring(msg string) <-chan string {c := make(chan string)go func() {for i := 0; ; i++ {c <- fmt.Sprintf("%s %d", msg, i)t := time.Duration(rand.Intn(1e3)) * time.Millisecondtime.Sleep(t)}}()return c}The output of this code is:foo 0bar 0foo 1bar 1foo 2bar 2foo 3bar 3foo 4bar 4FinishedBut how do I make the output less sequential? Or is that even needed. The channels do appear to be context switching as per how concurrency works. Maybe I'm just concerned how performant this type of concurrency is in relation to how threads typically work.
If you use buffered channels, it is closer to what you want: http://play.golang.org/p/uW1lw2TIww