Created
November 9, 2018 10:44
-
-
Save anatoliyfedorenko/ee54b2089d10893ea7c6d4834f3e1beb to your computer and use it in GitHub Desktop.
How to exit goroutine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "sync" | |
func main() { | |
var wg sync.WaitGroup | |
wg.Add(1) | |
ch := make(chan int) | |
go func() { | |
for { | |
foo, ok := <-ch | |
if !ok { | |
println("done") | |
wg.Done() | |
return | |
} | |
println(foo) | |
} | |
}() | |
ch <- 1 | |
ch <- 2 | |
ch <- 3 | |
ch <- 4 | |
close(ch) | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment