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

时间:2014-10-26 23:49:29

标签: arrays io go

我需要创建切片[]*bytes.Buffer的MultiReader buffer_slice := ... (type []*bytes.Buffer)
当我写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。抱歉我的英语不好。

1 个答案:

答案 0 :(得分:3)

您的切片应为[]io.Reader

b1 := &bytes.Buffer{}
b2 := &bytes.Buffer{}
buffers := []io.Reader{b1, b2}

multi := io.MultiReader(buffers...)