It works, but is generally a bad idea(tm)
your future software maintainers will hate you for it
Note, if those loops are not exactly the same, then the app will crash with when the main goroutine blocks due to noone else writing or reading
package main
import (
"fmt"
)
func main() {
c := make(chan int)
go func() {
for x := 0; x < 5; x++ {
var i int
select {
case c <- 1:
i = <-c
case i = <-c:
c <- 1
}
fmt.Println(" A - Written 1 red ", i)
}
}()
for x := 0; x < 5; x++ {
var i int
select {
case c <- 2:
i = <-c
case i = <-c:
c <- 2
}
fmt.Println(" B - Written 2 red ", i)
}
}
Output:
B - Written 2 red 1
A - Written 1 red 2
B - Written 2 red 1
A - Written 1 red 2
B - Written 2 red 1
A - Written 1 red 2
B - Written 2 red 1
A - Written 1 red 2
B - Written 2 red 1
Program exited.