I have an assignment to use arrays of channels.
I do not understand. Why this does not work?
package main
import "fmt"
func run() {
chann[0] <- 1
}
var chann = make([]chan int, 2)
func main() {
go run()
obj := <- chann[0]
fmt.Println(obj)
}
You have initialized the array of channels, but not the channels themselves.
As for why it deadlocks; this is due to the fact that a channel value that has not been initalised, or has been set to nil will always block. (See this article)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With