Go Concurrency and Channel Confusion
Problem
A user is trying to understand Go concurrency and channels using the following code snippet:
<code class="go">package main import "fmt" func display(msg string, c chan bool) { fmt.Println("display first message:", msg) c <- true } func sum(c chan bool) { sum := 0 for i := 0; i < 10000000000; i++ { sum++ } fmt.Println(sum) c <- true } func main() { c := make(chan bool) go display("hello", c) go sum(c) <-c }</code>
The expected output is only "display first message: hello" because the main function should exit once it receives data from the channel. However, the actual output also includes the sum of 10 billion numbers.
Answer
The main issue in the code is that the scheduler can choose freely between the two goroutines (display and sum) that are not blocked. While the programmer expects display to finish first and send data to the channel before sum completes, this may not happen due to the non-deterministic nature of the scheduler.
In one possible execution scenario:
To address this issue and ensure that the "display first message: hello" message is printed exclusively, one approach is to use a result channel to receive the message from display and terminate the program immediately. The modified main function would be:
<code class="go">func main() { result := make(chan string) go display("hello", result) fmt.Println(<-result) }</code>
The above is the detailed content of Why does my Go code print the sum of 10 billion numbers instead of just \'display first message: hello\'?. For more information, please follow other related articles on the PHP Chinese website!