Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of channels

Tags:

go

channel

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)
}
like image 994
Gytis S Avatar asked Jan 17 '14 19:01

Gytis S


1 Answers

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)

like image 112
Dmitri Goldring Avatar answered Sep 20 '22 19:09

Dmitri Goldring