Because the channel is captured by reference, so when the Go routine runs it already has the later value.
Your code has a race condition - run it under the race detector. > On Sep 19, 2024, at 11:30 AM, Feng Liyuan (SunRunAway) > <darktempla...@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+unsubscr...@googlegroups.com > <mailto:golang-nuts+unsubscr...@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/A10FA8D9-B5DF-4F56-B37C-5E21A2C5B591%40ix.netcom.com.