I am trying to pass a array of channels to the method " func Data(channel chan<- []Book)" , however I encouter an error which states ( channel[0] (type chan<- []Book does not support indexing)") I understand what it means, but isn't there a way to pass an the array ? If so what alternatives do I have
func Data(channel chan<- []Book) {
var data EData
data = ReadJSONFile("Data.json")
go Writer(data.BookStores[0].Central, channel[0]) // at this
// place I get "invalid operation: channel[0] (type chan<- []Book
// does not support indexing)"
}
The proper type to pass a slice of channels of Book elements is:
[]chan<- Book
The code in your original question is for a channel of Book slices.
chan<- []Book
needs to become []chan<- Book
. []
modifies the type after it in go, so if you want an array of channels, put it before chan
.
func Data(channel []chan<- Book) {
var data EData
data = ReadJSONFile("Data.json")
go Writer(data.BookStores[0].Central, channel[0])
// ...
}