Home > Enterprise >  How to read arbitrary amounts of data directly from a file in Go?
How to read arbitrary amounts of data directly from a file in Go?

Time:10-30

Without reading the contents of a file into memory, how can I read "x" bytes from the file so that I can specify what x is for every separate read operation?

I see that the Read method of various Readers takes a byte slice of a certain length and I can read from a file into that slice. But in that case the size of the slice is fixed, whereas what I would like to do, ideally, is something like:

func main() {
    f, err := os.Open("./file.txt")
    if err != nil {
        panic(err)
    }

    someBytes := f.Read(2)
    someMoreBytes := f.Read(4)
}

bytes.Buffer has a Next method which behaves very closely to what I would want, but it requires an existing buffer to work, whereas I'm hoping to read an arbitrary amount of bytes from a file without needing to read the whole thing into memory.

What is the best way to accomplish this?

Thank you for your time.

CodePudding user response:

you can do something like this:

f, err := os.Open("/tmp/dat")
check(err)
b1 := make([]byte, 5)
n1, err := f.Read(b1)
check(err)
fmt.Printf("%d bytes: %s\n", n1, string(b1[:n1]))

for more reading please check site.

CodePudding user response:

Use this function:

// readN reads and returns n bytes from the reader.
// On error, readN returns the partial bytes read and
// a non-nil error.
func readN(r io.Reader, n int) ([]byte, error) {
    b := make([]byte, n)        // Allocate buffer for result
    n, err := io.ReadFull(r, b) // ReadFull ensures buffer is filled or error is returned.
    return b[:n], err
}

Call like this:

someBytes, err := readN(f, 2)
if err != nil  { /* handle error here */
someMoreBytes := readN(f, 4)
if err != nil  { /* handle error here */
  • Related