Created
August 10, 2014 15:28
-
-
Save tenntenn/d34b0a78c866768fce14 to your computer and use it in GitHub Desktop.
チャネルに対するcapとlenについて #golang ref: http://qiita.com/tenntenn/items/686a75e11e8dcd9912ec
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
// 容量0 | |
ch1 := make(chan int) | |
// 容量10 | |
ch2 := make(chan int, 10) |
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
ch := make(chan int, 1) | |
ch <- 100 // ブロックしない | |
ch <- 200 // ブロックする |
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
ch1 := make(chan int, 10) | |
// 10 | |
fmt.Println(cap(ch1)) | |
ch2 := make(chan int) | |
// 0 | |
fmt.Println(cap(ch2)) |
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
ch := make(chan struct{}, 10) | |
ch <- struct{}{} | |
// cap: 10 len: 1 | |
fmt.Println("cap:", cap(ch), "len:", len(ch)) | |
<-ch | |
// cap: 10 len: 0 | |
fmt.Println("cap:", cap(ch), "len:", len(ch)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment