I am quite new to Golang. I wrote the following code for practice and ran into a run-time error message of a deadlock:
package main
import (
"fmt"
)
func system(WORKERS int) {
fromW := make(chan bool)
toW := make(chan bool)
for i := 0; i != WORKERS; i++ {
go worker(toW, fromW)
}
coordinator(WORKERS, fromW, toW)
}
func coordinator(WORKERS int, in, out chan bool) {
result := true
for i := 0; i != WORKERS; i++ {
result = result && <-in
}
for i := 0; i != WORKERS; i++ {
out <- result
}
fmt.Println("%t", result)
}
func worker(in, out chan bool) {
out <- false
<-in
}
func main() {
system(2)
}
However, if I swap the operands of && in line 19 to have
result = <-in && result,
the code works properly without returning any error messages. How can I explain this behaviour? Am I missing anything here? The OS I use is Windows 10, and the Golang version is 1.8.3.
Thank you very much in advance.