Never start a goroutine without knowing how it will stop | Dave Cheney
In Go, goroutines are cheap to create and efficient to schedule. The Go runtime has been written for programs with tens of thousands of goroutines as the norm, hundreds of thousands are not unexpected. But goroutines do have a finite cost in terms of memory footprint; you cannot create an infinite number of them. Every time you use the go keyword in your program to launch a goroutine, you must know how, and when, that goroutine will exit. If you don’t know the answer, that’s a potential memory leak. Consider this trivial code snippet: This code obtains a channel of int from somefunction and starts a goroutine to drain it. When will this goroutine exit? It will only exit when ch is closed. When will that occur? It’s hard to say, ch is returned by somefunction. So, depending on the state of somefunction, ch might never be closed, causing the goroutine to quietly leak. In your design, some goroutines may run until the program exits, for example a background goroutine watching a configurat
In Go, goroutines are cheap to create and efficient to schedule. The Go runtime has been written for programs with tens of thousands of goroutines as the norm, hundreds of thousands are not unexpected. But goroutines do have a finite cost in terms of memory footprint; you cannot create an infinite number of them. Every time you use the go keyword in your program to launch a goroutine, you must know how, and when, that goroutine will exit. If you don’t know the answer, that’s a potential memory leak. Consider this trivial code snippet: ch := somefunction() go func() { for range ch {
Explore this link on the map →related reading
- Frequently Asked Questions (FAQ) - The Go Programming Languagego.dev
- Notes on structured concurrency, or: Go statement considered harmful - njs blogvorpus.org
- A Guide to the Go Garbage Collector - The Go Programming Languagego.dev
- Goroutines - Concurrent Programming in Goprogramiz.com
- Memory Allocation in Go | Melatoninghiant3223.github.io
- The Go Memory Model - The Go Programming Languagego.dev
- Go go-to guide · YourBasicyourbasic.org
- Deep Dive in Go Channels | Michalis Zampetakismzampetakis.com
- Go at Google: Language Design in the Service of Software Engineering - The Go Programming Languagego.dev
- Peter Bourgon · Go for Industrial Programmingpeter.bourgon.org
- Writing Better Go: Lessons from 10 Code Reviews - Speaker Deckspeakerdeck.com
- Writing Better Go: Lessons from 10 Code Reviews - Speaker Deckspeakerdeck.com