watain
watain

Reputation: 5098

Wrap io.PipeReader to store progress

I would like to keep track of the progress in an io.Pipe. I came up with the following struct ProgressPipeReader which wraps io.PipeReader, storing the progress in bytes inside 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
}

Unfortunately, I can't seem to use it to wrap an io.PipeReader. The following snippet

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

yields the error

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

Any hints?

Upvotes: 2

Views: 172

Answers (1)

user142162
user142162

Reputation:

As the error describes, you are trying to store a *io.PipeReader value in a io.PipeReader field. You can fix this by updating your struct definition to the correct type:

type ProgressPipeReader struct {
        *io.PipeReader
        progress int64
}

Upvotes: 2

Related Questions