4 ms·
Eh, the real answer is close twice (as in the article update): func f(filename string) error { fp, err := os.Create(filename) if err != nil { r
by klodolph 2y ago
Eh, the real answer is close twice (as in the article update):
func f(filename string) error {
fp, err := os.Create(filename)
if err != nil {
return err
}
defer fp.Close()
if _, err := fmt.Fprintln(fp, "Hello, world!"); err != nil {
return err
}
return fp.Close() // safe to call multiple times
}
The .Close() method is safe to call multiple times. This behavior is documented on the os.File