Channel Not Closed, Program Stalled
In your Go application using sync.WaitGroup and channels, you've encountered an issue where the program doesn't exit even after waiting for all goroutines to complete.
Problem Details
Your code utilizes a WaitGroup to track goroutine completion and a channel to transmit fetched symbol names. However, the fetchedSymbols channel remains open indefinitely, preventing the loop in main from terminating.
Solution
<code class="go">go func() { wg.Wait() close(fetchedSymbols) }()</code>
<code class="go">for { select { case symbol := <-fetchedSymbols: fmt.Println("fetched", symbol) } }</code>
With these modifications, your code will now correctly exit after all symbol quotes are fetched and stored.
The above is the detailed content of Here are a few question-based titles that fit your provided text: * **Go WaitGroup and Channels: Why Does My Program Not Exit?** * **Stuck in a Loop: How to Properly Close Channels in Go with WaitGro. For more information, please follow other related articles on the PHP Chinese website!