Created
August 27, 2014 12:57
-
-
Save kamatama41/0c98fe311acd63f116fa to your computer and use it in GitHub Desktop.
go初心者がgorutineとChannelを使ってみる ref: http://qiita.com/kamatama_41/items/466c7a26069e1c5c6212
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" | |
"time" | |
) | |
func putChannel(c chan int, count int) { | |
for i := 0; i < count; i++ { | |
c <- i | |
fmt.Printf("add %d...\n", i) | |
time.Sleep(100 * time.Millisecond) | |
} | |
close(c) | |
} | |
func main() { | |
c := make(chan int, 5) | |
go putChannel(c, 10) | |
time.Sleep(600 * time.Millisecond) | |
for i := range c { | |
fmt.Println(i) | |
} | |
} |
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
add 0... | |
add 1... | |
add 2... | |
add 3... | |
add 4... | |
0 | |
1 | |
2 | |
3 | |
4 | |
add 5... | |
5 | |
add 6... | |
6 | |
add 7... | |
7 | |
add 8... | |
8 | |
add 9... | |
9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment