英文:
How can I push bytes into a reader in Go?
问题
我需要将接收到的字节逐步追加到一个缓冲区中,以便以后可以再次读取。类似这样的代码:
type TwoPassReader struct {
someDataSource SomeDataReader
secondPassReader io.Reader
}
func (r *TwoPassReader) Read(b []byte) (n int, err error) {
n, err := r.someDataSource.Read(b)
if err == nil {
//XXX: 这个实际上不存在,对吗?
bytesAppended, writeErr := r.secondPassReader.Write(b)
}
return n, err
}
func (r *) ReadSecondPass(b []byte) (n int, err error) {
return r.secondPassReader.Read(b)
}
虽然最简单的解决方案是使用bufio.Reader.Peek
,但我有一个无法修改的SomeDataReader
对象,而不是一个Reader。将数据放入[]byte
并在调用ReadSecondPass
时返回,是最好的解决方案吗?
英文:
I need to incrementally append bytes as I receive them to a buffer where I can read them again later. Something like this:
type TwoPassReader struct {
someDataSource SomeDataReader
secondPassReader io.Reader
}
func (r *TwoPassReader) Read(b []byte) (n int, err error) {
n, err := r.someDataSource.Read(b)
if err == nil {
//XXX: this doesn't actually exist, right?
bytesAppended, writeErr := r.secondPassReader.Write(b)
}
return n, err
}
func (r *) ReadSecondPass(b []byte) (n int, err error) {
return r.secondPassReader.Read(b)
}
While the trivial answer for this would be to use bufio.Reader.Peek
, I have a SomeDataReader
object that I can't modify instead of a Reader. Is the best solution to just put the data into a []byte
and return that when ReadSecondPass
is called?
答案1
得分: 2
像这样吗?
package main
import (
"bytes"
"io"
)
type TwoPassReader struct {
firstPassReader io.Reader
secondPassBuffer io.ReadWriter
}
func NewTwoPassReader(firstPass io.Reader) *TwoPassReader {
return &TwoPassReader{
firstPassReader: firstPass,
secondPassBuffer: bytes.NewBuffer(nil),
}
}
func (r *TwoPassReader) Read(b []byte) (n int, err error) {
n1, err1 := r.firstPassReader.Read(b)
n2, err2 := r.secondPassBuffer.Write(b[:n1])
if err2 != nil {
return n2, err2
}
return n1, err1
}
func (r *TwoPassReader) ReadSecondPass(b []byte) (n int, err error) {
return r.secondPassBuffer.Read(b)
}
func main() {
}
这是一个示例的Go语言代码。它定义了一个名为TwoPassReader
的结构体,该结构体具有两个字段:firstPassReader
和secondPassBuffer
。TwoPassReader
结构体还有两个方法:Read
和ReadSecondPass
,用于读取数据。
NewTwoPassReader
函数用于创建一个TwoPassReader
对象,并初始化其字段。
在main
函数中,你可以添加你的代码逻辑。
英文:
Something like this?
package main
import (
"bytes"
"io"
)
type TwoPassReader struct {
firstPassReader io.Reader
secondPassBuffer io.ReadWriter
}
func NewTwoPassReader(firstPass io.Reader) *TwoPassReader {
return &TwoPassReader{
firstPassReader: firstPass,
secondPassBuffer: bytes.NewBuffer(nil),
}
}
func (r *TwoPassReader) Read(b []byte) (n int, err error) {
n1, err1 := r.firstPassReader.Read(b)
n2, err2 := r.secondPassBuffer.Write(b[:n1])
if err2 != nil {
return n2, err2
}
return n1, err1
}
func (r *TwoPassReader) ReadSecondPass(b []byte) (n int, err error) {
return r.secondPassBuffer.Read(b)
}
func main() {
}
答案2
得分: 1
io.Reader
只是一个接口。我们无法看到你将什么赋值给该结构字段(如果有的话),但是在这里使用bytes.Buffer可能是正确的类型。
英文:
io.Reader
is just an interface. We can't see what you're assigning to that struct field (if anything), but a bytes.Buffer would probably be the correct type to use here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论