I am reading Learning Go book and in chapter 10 the author suggests a way to avoid the deadlock with the use of select statement.
package main
import (
"fmt"
)
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
go func() {
v := 1
ch1 <- v
v2 := <-ch2
fmt.Println(v, v2)
}()
v := 2
var v2 int
select {
case ch2 <- v:
case v2 = <-ch1:
}
fmt.Println(v, v2)
}
The output that I get always in Go playground is 2 1.
This output is coming from main goroutine. Why don't I see anything from the anonymous function?
Is every case in select getting executed?
Also could someone please explain in greater detail the execution flow? Thanks in advance.