package main import ( "fmt" "sync" "time" ) func printChannel(ci <-chan int, done chan<- bool) { defer func() { done <- true }() for i := range ci { time.Sleep(100 * time.Millisecond) fmt.Println(i) } } func printWaitgroup(ci <-chan int, wg *sync.WaitGroup) { defer func() { wg.Done() }() for i := range ci { time.Sleep(100 * time.Millisecond) fmt.Println(i) } } func main() { c := make(chan int) //done := make(chan bool) //go printChannel(c, done) wg := sync.WaitGroup{} wg.Add(1) go printWaitgroup(c, &wg) c <- 1 c <- 2 c <- 3 close(c) //<-done wg.Wait() }