Skip to content

Instantly share code, notes, and snippets.

@gsavovski
Created October 28, 2014 21:37
Show Gist options
  • Save gsavovski/79e8aee4115b144b2df6 to your computer and use it in GitHub Desktop.
Save gsavovski/79e8aee4115b144b2df6 to your computer and use it in GitHub Desktop.
Why the lenght of a channel without initialized capacity is 0?
package main
import "fmt"
import "time"
func main() {
c := make(chan int)
go func() {
for i := 0; i < 10; i += 1 {
c <- i
fmt.Println("sent:", i)
time.Sleep(10 * time.Millisecond)
}
}()
for {
fmt.Println("len(c): ", len(c), cap(c))
x := <-c
time.Sleep(100 * time.Millisecond)
fmt.Println(" got:", x)
if x > 8 {
break
}
}
close(c)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment