英文:
Golang create multireader of bytes.Buffer array
问题
我需要创建一个[]*bytes.Buffer
切片的MultiReader。
当我写io.MultiReader(buffer_slice)
时,我得到了错误:
cannot use buffer_slice (type []*bytes.Buffer) as type io.Reader in argument to io.MultiReader: []*bytes.Buffer does not implement io.Reader (missing Read method).
但是函数签名是MultiReader(readers ...Reader) Reader
。
我理解传递数组是没有意义的,实际问题是:除了使用循环之外,还有其他选项吗?
P.S. 对不起,我的英语不好。
英文:
I need Create MultiReader of slice []*bytes.Buffer
buffer_slice := ... (type []*bytes.Buffer)
When i write io.MultiReader(buffer_slice)
I get the error:
cannot use buffer_slice (type []*bytes.Buffer) as type io.Reader in argument to io.MultiReader: []*bytes.Buffer does not implement io.Reader (missing Read method).
But function signature MultiReader(readers ...Reader) Reader
I understand that the transmit array is meaningless, the actual question: besides the trivial cycle, there are no more options?
P.S. Sorry for my bad english.
答案1
得分: 4
你的切片应该是[]io.Reader
类型的。
b1 := &bytes.Buffer{}
b2 := &bytes.Buffer{}
buffers := []io.Reader{b1, b2}
multi := io.MultiReader(buffers...)
英文:
Your slice should be type []io.Reader
b1 := &bytes.Buffer{}
b2 := &bytes.Buffer{}
buffers := []io.Reader{b1, b2}
multi := io.MultiReader(buffers...)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论