See when we create w0 goroutine then a new thread will be generated so now we have 2 thread one main another w0 now from w0 we can w which will run of w0 thread and run sequentially so program will not execute in w0 till w not returned then ur channel close now at the time u have executed the w the function w keeps sending data to channel. now on that time main goroutine will wait untill the channel closes and which eventually going to close after close function after w returns to w0. So its the complete flow
On Mon, 8 Jun, 2026, 4:04 pm 'andreas graeper' via golang-nuts, < [email protected]> wrote: > hi. thanks for your answers. i understand that the channel has to be > closed. what i do not understand is the error 'write after close' > if w (called from w0) was executed in another thread, than i had to wait > for the end of w by some mean of synchronization, before i close the > channel. but if w0 and w are executed in same thread they are executed > sequentielly inside the thread and close inside w0 is called when w has > returned. so it should be fine. in main then for v := range c { } would > end. > > andreas graeper schrieb am Montag, 8. Juni 2026 um 11:42:37 UTC+2: > >> 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/8b343e2a-ab29-4c33-8f3f-beb5b61ae09dn%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/8b343e2a-ab29-4c33-8f3f-beb5b61ae09dn%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/CAEawHbQE%3DuSqPiFuhTZLna5HAjA06igQbKBYWjxCW0Mvn4M97w%40mail.gmail.com.
