Golang创建bytes.Buffer数组的多读取器

huangapple go评论92阅读模式
英文:

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...)

huangapple
  • 本文由 发表于 2014年10月27日 07:49:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/26579332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定