On Sat, Nov 27, 2021 at 11:03 AM Brian Candler <b.cand...@pobox.com> wrote:

> 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
>

That is not true <https://go.dev/play/p/NlnCjMA3n3O>.

FWIW I think the code in the blog post is bad, as it doesn't check the
errors from `Close`. But a) it's an old post, I don't know if it's worth
fixing it and b) fixing it might distort the point - teaching the mechanics
of defer.

The best way to fix it would probably be to do `return written,
dst.Close()` - i.e. call `Close()` twice.


> 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
> https://www.youtube.com/watch?v=74c19hwY2oE
> https://wiki.postgresql.org/wiki/Fsync_Errors
> # 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+unsubscr...@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
> <https://groups.google.com/d/msgid/golang-nuts/774265d3-f574-4bf7-bef7-ece3cd79643bn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfE2Tm26h0VOb1L03%3DenG1vnbVB0qeSZKaC1vjR3ZvohyA%40mail.gmail.com.

Reply via email to