Skip to content

Instantly share code, notes, and snippets.

@anatoliyfedorenko
Created November 9, 2018 10:44
Show Gist options
  • Save anatoliyfedorenko/ee54b2089d10893ea7c6d4834f3e1beb to your computer and use it in GitHub Desktop.
Save anatoliyfedorenko/ee54b2089d10893ea7c6d4834f3e1beb to your computer and use it in GitHub Desktop.
How to exit goroutine
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