Skip to content

Instantly share code, notes, and snippets.

@cchamplin
Last active March 22, 2016 20:00
Show Gist options
  • Save cchamplin/e96e144bdc9e9836ac13 to your computer and use it in GitHub Desktop.
Save cchamplin/e96e144bdc9e9836ac13 to your computer and use it in GitHub Desktop.
f, err := os.Open(path)
defer f.Close()
if err != nil {
log.Error.Printf("Failed to open file %s", path)
return "", err
}
byteData := make([]byte, 12)
bytesRead, err := f.Read(byteData)
if err != nil {
log.Error.Printf("File read error occurred for %s - read %d bytes", path, bytesRead)
fi, err := os.Stat(path)
if err != nil {
log.Error.Printf("Could not stat file %s : %v", path, err)
} else {
log.Error.Printf("File info for %s: Size: %d Mode: %v", path, fi.Size(), fi.Mode())
}
// This fails
bytesRead, err = f.Read(byteData)
if err != nil {
log.Error.Printf("Second read failed %s: %v", path, err)
f.Close()
f, err = os.Open(path)
if err != nil {
log.Error.Printf("Failed to open file %s", path)
return "", err
}
// This succeeds
bytesRead, err = f.Read(byteData)
if err != nil {
log.Error.Printf("Third read failed %s: %v", path, err)
} else {
// This succeeds
log.Error.Printf("Third read succeeded for %s %d", path, bytesRead)
}
} else {
log.Error.Printf("Second read succeeded for %s %d", path, bytesRead)
}
return "", err
}
// Output
// ERROR: 2016/03/22 13:54:59 image_util.go:23: File read error occurred for c:\GoWork\src\github.com\cchamplin\goimage\test\catalog\product\1\0\1002458_4.jpg - read 0 bytes
// ERROR: 2016/03/22 13:54:59 image_util.go:28: File info for c:\GoWork\src\github.com\cchamplin\goimage\test\catalog\product\1\0\1002458_4.jpg: Size: 53416 Mode: -rw-rw-rw-
// ERROR: 2016/03/22 13:54:59 image_util.go:32: Second read failed c:\GoWork\src\github.com\cchamplin\goimage\test\catalog\product\1\0\1002458_4.jpg: EOF
// ERROR: 2016/03/22 13:54:59 image_util.go:43: third read succeeded for c:\GoWork\src\github.com\cchamplin\goimage\test\catalog\product\1\0\1002458_4.jpg 12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment