go - 如何传递通道数组
问题描述
我正在尝试将一组通道传递给方法“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)"
}
解决方案
传递 Book 元素通道切片的正确类型是:
[]chan<- Book
您原始问题中的代码适用于书籍切片的频道。
推荐阅读
- python - 带有文本的 Pandas Dataframe 和编码某些字符的问题
- javascript - 具有加入服务器要求的 Discord js Giveaway 命令
- php - 使用 FILTER_SANITIZE_FULL_SPECIAL_CHARS 后使用 & 验证 RTF 输入
- c++ - 递归幂函数健全性检查
- python - Conda env 和 python 包
- c# - 在假的(FakeItEasy 库)上设置属性值似乎不起作用
- regex - 正则表达式从调用函数时使用的源代码中提取所有字符串
- vba - 计算记录比较的唯一标识符
- python - NEST 模拟器:python 说“ModuleNotFoundError: No module named 'nest'”
- javascript - 用新行分割,但如果它被引号括起来则跳过