Created
January 14, 2017 12:15
-
-
Save rodkranz/90c82583987a15e3d0f2c4678f2835c7 to your computer and use it in GitHub Desktop.
Read big file by chunk in go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
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
No need for bufio if you are using Read. File is already a Reader implementation.