func-e icon indicating copy to clipboard operation
func-e copied to clipboard

The issue of deferred io.Closer Close()

Open codefromthecrypt opened this issue 4 years ago • 1 comments

At least in go 1.17 amd64, there's a lot more assembly generated by the compiler for

func Close(src io.Closer) error {
	defer src.Close()
    return nil
}

vs doing it explicitly like this:

func Close(src io.Closer) error {
	defer func() { _ = src.Close()}()
    return nil
}

Note: src.Close() and _ = src.Close() make the same assembly.

https://godbolt.org/z/KP4q1WTah

Visual clutter of defer func() { _ = src.Close()}() is distracting, but if there's no plan to fix the go compiler to be more optimal, it could be better to handle this manually with a trailing comment of the issue left unsolved.

ex.

defer func() { _ = src.Close()}() // wrap manually to avoid assembly bloat until goland/go#12356

In func-e, this may be less important, but we should be considerate that this one is more than just style.

codefromthecrypt avatar Nov 18 '21 00:11 codefromthecrypt

Added this before we take any action as it might suggest this will be fixed or not ever fixed and that changes what we'd do https://github.com/golang/go/issues/49656

codefromthecrypt avatar Nov 18 '21 00:11 codefromthecrypt