Skip to content

Instantly share code, notes, and snippets.

@rodkranz
Created January 14, 2017 12:15
Show Gist options
  • Save rodkranz/90c82583987a15e3d0f2c4678f2835c7 to your computer and use it in GitHub Desktop.
Save rodkranz/90c82583987a15e3d0f2c4678f2835c7 to your computer and use it in GitHub Desktop.
Read big file by chunk in go
// Copyright 2017 Kranz. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package main
import (
"os"
"log"
"io"
"bufio"
)
func main() {
fileName := "data.json"
f, err := os.Open(fileName)
if err != nil {
log.Fatalf("Error to read [file=%v]: %v", fileName, err.Error())
}
nBytes, nChunks := int64(0), int64(0)
r := bufio.NewReader(f)
buf := make([]byte, 0, 4*1024)
for {
n, err := r.Read(buf[:cap(buf)])
buf = buf[:n]
if n == 0 {
if err == nil {
continue
}
if err == io.EOF {
break
}
log.Fatal(err)
}
nChunks++
nBytes += int64(len(buf))
// process buf
if err != nil && err != io.EOF {
log.Fatal(err)
}
}
log.Println("Bytes:", nBytes, "Chunks:", nChunks)
}
@jgardona
Copy link

jgardona commented Dec 6, 2023

No need for bufio if you are using Read. File is already a Reader implementation.

@stanleyyzhu
Copy link

I think you forgot to close the file

f, err := os.Open(fileName)
if err != nil {
	log.Fatalf("Error to read [file=%v]: %v", fileName, err.Error())
}
defer f.Close()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment