On Monday, 8 June 2026 at 10:42:37 UTC+1 andreas graeper wrote:
w(t,c) { if t!=nil { w(t.left,c) ; c<-t.elem ; w(t.right,c) }}
w0(t,c) { w(t,c); close(c); }
main(){
go w0(t,c)
for e := range c {}
}
Note that "for e := range c {}" will keep reading elements from c *until
the channel is closed*. Therefore it's necessary to close the channel,
after you've sent all elements on the channel - which is what you do in
your w0() function.
> it feels as if w is send to background too and i have to sync to the end
of w before i close the channel ?
You can only close a channel *after* all goroutines have finished writing
on it; otherwise you'll get the "write after close" panic that you
describe. Your w0() function waits for w() to return, therefore all sends
are complete, then closes the channel. That's fine.
The code you've sent is incomplete (it's missing types and keywords like
"func"), but if I finish it, it seems to work:
https://go.dev/play/p/9jPj7dCi1nA
Therefore if it's not working, I suggest you make a self-contained example
at https://go.dev/play/ which demonstrates your problem, and post the link
here.
--
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 [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/golang-nuts/1a1578b8-9803-4a44-bde7-9665b1af2f78n%40googlegroups.com.