Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What happens to the anonymous goroutine running in the background? #2

Open
BSardorbek opened this issue May 13, 2023 · 1 comment
Open

Comments

@BSardorbek
Copy link

BSardorbek commented May 13, 2023

https://github.com/kevchn/go-concurrency-patterns/blob/9493f7e9f5db056eae29c2faa231ea54c5514545/1-8-timeout-select.go#L29


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
}
@younesious
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants