.Close
method GO-S2307 160 return err
161 }
162
163 defer res.Body.Close() 164
165 if _, err := io.Copy(w, res.Body); err != nil {
166 return err
158 if err != nil {
159 return nil, err
160 }
161 defer res.Body.Close()162
163 id := res.Header.Get("Release-Id")
164
136 if err != nil {
137 return nil, err
138 }
139 defer res.Body.Close()140
141 id := res.Header.Get("Release-Id")
142
12 if err != nil {
13 return "", err
14 }
15 defer res.Body.Close()16
17 var auth struct {
18 Id string
80}
81
82func subscribeLogs(ctx context.Context, w io.WriteCloser, stream string, opts structs.LogsOptions) {
83 defer w.Close() 84
85 ch := make(chan logstorage.Log, 1000)
86
Calling Close()
method may return an error, and ignoring the same
might result in a data loss. This is similar to many more Close
methods.
For example, on POSIX systems, os.Close
uses the close
system call underneath,
which may return an EIO
:
A previously-uncommitted write(2) encountered an input/output error.
This means that the data written by os.Write
hasn't been written to the disk,
which results in a data loss. It is recommended to handle the error returned
by the os.Close
call or call os.Sync
if available to force the OS to write
the data to the disk.
package main
import (
"fmt"
"os"
)
func foo() error {
f, err := os.Create("/tmp/test.txt")
if err != nil {
return err
}
defer f.Close()
return fmt.Fprint(f, "Hello World")
}
package main
import (
"fmt"
"os"
)
func foo() error {
f, err := os.Create("/tmp/test.txt")
if err != nil {
return err
}
err = fmt.Fprint(f, "Hello World")
if err != nil {
return err
}
return f.Close()
}
package main
import (
"fmt"
"os"
)
func foo() error {
f, err := os.Create("/tmp/test.txt")
if err != nil {
return err
}
defer f.Close()
err = fmt.Fprint(f, "Hello World")
if err != nil {
return err
}
return f.Sync()
}