goroutine leak with buffered channel in Go -


th following codes in the go programming language

func mirroredquery() string {     responses := make(chan string, 3)     go func() { responses <- request("asia.gopl.io") }()     go func() { responses <- request("europe.gopl.io") }()     go func() { responses <- request("americas.gopl.io") }()     return <-responses // return quickest response }  func request(hostname string) (response string) { /* ... */ } 

and book says

had used unbuffered channel, 2 slower goroutines have gotten stuck trying send responses on channel no goroutine ever receive . situation, called goroutine leak, bug . unlike garbage variables, leaked goroutines not automatically collec ted, important make sure goroutines terminate when no longer needed.


, question why situation cause goroutine leak.in idea, buffered channel's cap 3, , 3 goroutines send requests , exit not cause leak.

the code shown not cause leak.

as paragraph states:

had used unbuffered channel

meaning: if had used unbuffered channel...

so if channel unbuffered leak occur.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -