Skip to content

Instantly share code, notes, and snippets.

@suzuken
Created April 5, 2016 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suzuken/2104d8dff6f6023ff0e415d13c95ff42 to your computer and use it in GitHub Desktop.
Save suzuken/2104d8dff6f6023ff0e415d13c95ff42 to your computer and use it in GitHub Desktop.
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