-
-
Save suzuken/2104d8dff6f6023ff0e415d13c95ff42 to your computer and use it in GitHub Desktop.
This file contains 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" | |
) | |
func main() { | |
ch := make(chan string, 3) | |
p(ch) // cap: 3, len: 0 | |
ch <- "A" | |
p(ch) // cap: 3, len: 1 | |
ch <- "B" | |
ch <- "C" | |
p(ch) // cap: 3, len: 3 | |
// これはsendがcapacityをこえ、かつreceiveされないのでdeadlockする | |
// ch <- "D" | |
fmt.Println(<-ch) // A | |
p(ch) // cap: 3, len: 2 | |
} | |
func p(ch chan string) { | |
fmt.Printf("cap: %d, len: %d\n", cap(ch), len(ch)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment