Yes you can because function are just code and the code you have mentioned
works fine as what you have mentioned that if you remove close then for
loop will run forever  as it might not able to find out whether the channel
still have something to send and wait as channels sends 2 thing value and
ok where okay tells whether the channels opened or close if you use close
that ok becomes false and for loop terminates

On Mon, 8 Jun, 2026, 3:13 pm 'andreas graeper' via golang-nuts, <
[email protected]> wrote:

> hi. i try to walk through a tree putting elems into channel
> 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 {}
> }
> if i drop 'close(c)' i get 'all threads dead' , otherwise 'write after
> close'
> 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 ?
> thanks in advance, andi
>
> --
> 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/4a665d05-d769-4249-a6df-4e3de0589d1an%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/4a665d05-d769-4249-a6df-4e3de0589d1an%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 [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/CAEawHbQTsRKyTM%3DaQMAmv7x4JBBaULH30AVK7%3D28vO6vqB94FQ%40mail.gmail.com.

Reply via email to