I am not clear about why the print statement in func a1
will not print anything if I remove <-result
in func a2
.
I think before we send something into result, the print statement should be executed, and <-result
should not have some effect on that.
func a2(){
x := 3
result := make(chan int, 10)
input := make(chan int, 10)
go a1(x, input, result)
input <- 4
<-result
}
func a1(x int, input <-chan int, result chan<- int){
y := <-input
fmt.Println("hello", y)
result <- x
}
However, then I tried the following code: it will print hello
no matter whether I have <-result
or not.
func a2(){
x := 3
result := make(chan int, 10)
go a1(x, result)
<-result
}
func a1(x int, result chan<- int){
fmt.Println("hello")
result <- x
}
Can you explain this in a detailed way so that a beginner can understand this? It seems like input <-chan int
this input channel is doing something that causes the difference.