Created
January 9, 2015 20:55
-
-
Save subh007/abea0016e01b63b67612 to your computer and use it in GitHub Desktop.
channel, goroutine, sync
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 ( | |
"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