Skip to content

bufio: add function to read a certain amount of bytes #17064

@n10v

Description

@n10v

In many programs, where I use bufio package, lacks the function, that can read certain amount of bytes.
So now I should create function like this:

func ReadSeveralBytes(r *bufio.Reader, n int) ([]byte, error) {
    if n == 0 || r == nil {
        return nil, nil
    }

    peeked, err := r.Peek(n)
    if err != nil {
        return nil, err
    }

    if _, err := r.Discard(n); err != nil {
        return nil, err
    }

    return peeked, nil
}

But it's inconvenient and I think, the way it works could be better.
I hope, what I'm not the only one, who wants it in bufio package.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions