首页 > 解决方案 > 如何传递通道数组

问题描述

我正在尝试将一组通道传递给方法“func Data(channel chan<- []Book)”,但是我遇到了一个错误,指出(channel[0](type chan<- []Book 不支持索引) )") 我明白它的意思,但没有办法传递数组吗?如果是这样,我有什么替代方案

     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)"
        }

标签: go

解决方案


传递 Book 元素通道切片的正确类型是:

[]chan<- Book

您原始问题中的代码适用于书籍切片的频道。


推荐阅读