using io.LimitReader to read a binary file

April 18, 2015   

I was working on a problem and wanted to read a very specific chunck of file. I read Go documentation and came through io.LimitReader. According to official documentation, “A LimitedReader reads from R but limits the amount of data returned to just N bytes. Each call to Read updates N to reflect the new amount remaining.” and this is what I wanted.


f, _ := os.Open("largefile.bin")
f.Seek(123, 0)

b := make([]byte, 150-123) // remaining length after seek

f.Read(&b)
f.Close()

buf := bytes.NewBuffer(b) // create buffer with the needed bytes

var a float32
binary.Read(buf, binary.LittleEndian, &a)

binary.Read(io.LimitReader(r, 150-123), binary.LittleEndian, &a)