I am very new to golang.
My understanding is, All the go-routines will be executed concurrently. both anonymous goroutines will start executing concurrently. but when i run this code, It always prints
a=1 first executed
a=1 second executed
panic: b != 1
Shouldnt it print
a = 1
a = 1 first executed
Response true
and so on
or
b =1
b = 1 first executed
Response true
and so on
Since after sending a value to the channel, the corresponding goroutine should block and wait for the receiver?
func main() {
var a, b int
var c = make(chan bool)
go func() {
b = 1
fmt.Println("b=1 first executed")
c <- true
fmt.Println("b=1 second executed")
if a != 1 { // impossible
panic("a != 1") // will never happen
}
fmt.Println("b=1 third executed")
}()
go func() {
a = 1
fmt.Println("a=1 first executed")
c <- true
fmt.Println("a=1 second executed")
if b != 1 { // impossible
panic("b != 1") // will never happen
}
fmt.Println("a=1 third executed")
}()
fmt.Println("Response ", <-c)
fmt.Println("Main executed")
}