.Close
method GO-S2307275 t.Error("Making the request:", err.Error())
276 return
277 }
278 defer resp.Body.Close()279 body, ioerr := io.ReadAll(resp.Body)
280 if ioerr != nil {
281 t.Error("Reading the response:", ioerr.Error())
106 t.Error("Making the request:", err.Error())
107 return
108 }
109 defer resp.Body.Close()110
111 body, ioerr := io.ReadAll(resp.Body)
112 if ioerr != nil {
50 if req.Body == nil {
51 return extractor.QueryFromBody(strings.NewReader(""))
52 }
53 defer req.Body.Close() 54 return extractor.QueryFromBody(req.Body)
55 }
56
43 if req.Body == nil {
44 return extractor.BodyFromBody(strings.NewReader(""))
45 }
46 defer req.Body.Close() 47 return extractor.BodyFromBody(req.Body)
48 }
49 generateQueryFn = func(req *Request) (url.Values, error) {
318 t.Error("Making the request:", err.Error())
319 return
320 }
321 defer resp.Body.Close()322 body, ioerr := io.ReadAll(resp.Body)
323 if ioerr != nil {
324 t.Error("Reading the response:", ioerr.Error())
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()
}