What happens to the anonymous goroutine running in the background?
BSardorbek opened this issue · comments
BSardorbek commented
func generator(msg string) <-chan string { // returns receive-only channel
ch := make(chan string)
go func() { // anonymous goroutine
for i := 0; ; i++ {
ch <- fmt.Sprintf("%s %d", msg, i)
time.Sleep(time.Second)
}
}()
return ch
}
Younes Mahmoudi commented
The goroutine, still running in the background, continues its infinite loop.
However, since the main code is no longer receiving from the channel, the goroutine's actions are effectively ignored.
The Go runtime will eventually clean up the goroutine when the program exits.