On Friday, September 20, 2024 at 8:52:46 AM UTC+12 robert engels wrote:

Because the channel is captured by reference, so when the Go routine runs 
it already has the later value.


Maybe this modified version better illustrates the problem:
https://go.dev/play/p/XLekzv9wsVO

The original code has no synchronization, so you cannot be sure when the 
goroutine will run and reference the channel from the outer scope. This 
updated example shows the difference between synchronizing before and after 
the goroutine.
 


Your code has a race condition - run it under the race detector.

On Sep 19, 2024, at 11:30 AM, Feng Liyuan (SunRunAway) <darkte...@gmail.com> 
wrote:

I have a piece of Go code where I create a buffered error channel and send 
an error to it from a goroutine. Then, in the main, I recreate this 
channel. To my confusion, even after recreating the channel, I still 
receive the error sent by the original goroutine. Here is the code:

https://go.dev/play/p/y3roOPKO0iy

Can someone help explain why this happens?

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an 
email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/3a49b432-a8fa-4bb3-bcd2-a8fcd679a0c3n%40googlegroups.com
 
<https://groups.google.com/d/msgid/golang-nuts/3a49b432-a8fa-4bb3-bcd2-a8fcd679a0c3n%40googlegroups.com?utm_medium=email&utm_source=footer>
.


-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/63fc3ce1-e016-480d-9df7-aa59c240b09bn%40googlegroups.com.

Reply via email to