--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/726e95ed-9b14-46a5-bb09-7821616f4ff9n%40googlegroups.com.
OK, I see. Thank you.
Hello,
actually trying this with os.Open() the program behaves the same regardless whether the defer is before or after the error handling, Thus, no panic :-)
Isn't anything declared with defer always running after the user
code in the given func ends? I am too tired now to look this up.
BR,
Roland
package main
import (
"fmt"
"os"
)
func main() {
f, err := os.Open("/tmp/dat")
//defer f.Close()
fmt.Printf("%v\n", f)
if err != nil {
fmt.Println("NO FILE")
//return
}
defer f.Close()
fmt.Println("END")
}
Output:
<nil>IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. --
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/PAXPR08MB6640963981E13AC180B4E19594629%40PAXPR08MB6640.eurprd08.prod.outlook.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/4d0b379f-c2a0-a314-e2d3-5fec36528845%40gmail.com.
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x47f812]
goroutine 1 [running]:
main.main()
/tmp/sandbox1681946220/prog.go:14 +0x112
panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x47f8d4] goroutine 1 [running]: main.(*Event).getS(0x4b2a40) /tmp/sandbox1571083836/prog.go:35 +0x14
BR,
Roland
Isn't the code should be:func CopyFile() {...if err != nil {return}defer func() { err = src.Close() }()...}
Whether (1) or (2) applies depends whether the final return is a "naked" one or not. In this case it isn't:return io.Copy(dst, src)Therefore, the value returned is whatever io.Copy() returns
There is a wider question as to whether you should check the error return from Close() in the first place. In some rare cases it *might* show something, e.g. Close() when you are *writing* a file may return disk full errors for some filesystems. However it doesn't give a guarantee that all errors are caught.If you need to guarantee that the data was successfully written to disk then you should call Fsync() and check the return value of that, but that's an expensive operation (it forces the OS to write dirty buffers to disk). Even then there are issues to beware of, especially if multiple threads are calling Fsync() on the same file. See# both linked from: https://docs.ceph.com/en/mimic/cephfs/posix/Most applications don't need this type of guarantee, i.e. if the disk becomes full or there are I/O errors then they accept that the data on disk will be corrupt.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/774265d3-f574-4bf7-bef7-ece3cd79643bn%40googlegroups.com.