Created
May 3, 2023 23:04
-
-
Save hnakamur/947c13eeb35452771858cc9372b902af to your computer and use it in GitHub Desktop.
verify Go os.File.Read returns 0, io.EOF at end of file.
This file contains 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
package main | |
import ( | |
"io" | |
"log" | |
"os" | |
) | |
func main() { | |
if err := run(); err != nil { | |
log.Fatal(err) | |
} | |
} | |
func run() error { | |
const filename = "hello.txt" | |
if err := os.WriteFile(filename, []byte("hello"), os.FileMode(0o600)); err != nil { | |
return err | |
} | |
defer os.Remove(filename) | |
file, err := os.Open(filename) | |
if err != nil { | |
return err | |
} | |
defer file.Close() | |
var buf [4]byte | |
for { | |
n, err := file.Read(buf[:]) | |
log.Printf("n=%d, err=%v", n, err) | |
if err == io.EOF { | |
break | |
} | |
if err != nil { | |
return err | |
} | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://pkg.go.dev/[email protected]#File.Read