Skip to content

Instantly share code, notes, and snippets.

@subh007
Created January 9, 2015 20:55
Show Gist options
  • Save subh007/abea0016e01b63b67612 to your computer and use it in GitHub Desktop.
Save subh007/abea0016e01b63b67612 to your computer and use it in GitHub Desktop.
channel, goroutine, sync
package main
import (
"fmt"
"sync"
)
func printNumber(i int) int {
fmt.Println(i)
return i
}
func main() {
wg := &sync.WaitGroup{}
ch := make(chan int)
for i := 0; i < 10; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
ch <- printNumber(i)
}(i)
}
go func() { wg.Wait(); close(ch) }()
for j := range ch {
fmt.Println(j)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment