I have a function which takes an int array and dumps them in a channel.
func Dump(a []int, ch chan int) {
for i := range a {
ch <- i
}
close(ch)
}
This main doesn't build:
func main() {
ch := make(chan int)
arr := []int{1, 2, 3, 4, 5}
Dump(arr, ch)
for i := range ch {
fmt.Printf("Got %v
", i)
}
}
throws this error:
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send]:
main.Dump(0xc000078f48, 0x5, 0x5, 0xc00006e060)
/Users/300041738/go-workspace/src/test.go:7 +0x43
main.main()
/Users/300041738/go-workspace/src/test.go:15 +0x9b
exit status 2
However, this builds:
func main() {
ch := make(chan int)
arr := []int{1, 2, 3, 4, 5}
go Dump(arr, ch)
for i := range ch {
fmt.Printf("Got %v
", i)
}
}
Why do I have to write go infront of Dump? I don't wish to dump the contents of the array asynchronously.