go - 包装 io.PipeReader 来存储进度

标签 go pipe

我想跟踪 io.Pipe 中的进度。我想出了以下结构 ProgressPipeReader ,它包装了 io.PipeReader,将进度以字节为单位存储在 ProgressPipeReader.progress 中:

type ProgressPipeReader struct {
        io.PipeReader
        progress int64
}

func (pr *ProgressPipeReader) Read(data []byte) (int, error) {
        n, err := pr.PipeReader.Read(data)

        if err == nil {
                pr.progress += int64(n)
        }

        return n, err
}

不幸的是,我似乎无法使用它来包装io.PipeReader。以下代码片段

pr, pw := io.Pipe()
pr = &ProgressPipeReader{PipeReader: pr}

产生错误

cannot use pr (type *io.PipeReader) as type io.PipeReader in field value

有什么提示吗?

最佳答案

正如错误所述,您尝试在 io.PipeReader 字段中存储 *io.PipeReader 值。您可以通过将结构定义更新为正确的类型来解决此问题:

type ProgressPipeReader struct {
        *io.PipeReader
        progress int64
}

关于go - 包装 io.PipeReader 来存储进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44759185/

相关文章:

docker - 如何在 docker go 客户端中创建具有内存限制的容器

go - Flatbuffers GoLang - 在序列化和反序列化数据时无法理解我的错误导致无法检索数据

c dup 未定义错误

c - execl 是否需要 dup2()

c - 写入 Fortran 和 C 程序之间的管道时出现问题

go - 为什么接口(interface)单元会出错

go - 如何使用Go执行“cat 7zSD.sfx.config.txtxxxx.7z> setup.exe”

go - 有没有什么方法可以用 Go 语言进行面向方面的编程

c - 如何将 execlp 更改为 execv 并运行其他命令然后只是 'ls'

linux - 从管道查找结果中检索最后修改的文件