Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The issue of deferred io.Closer Close() #414

Open
codefromthecrypt opened this issue Nov 18, 2021 · 1 comment
Open

The issue of deferred io.Closer Close() #414

codefromthecrypt opened this issue Nov 18, 2021 · 1 comment

Comments

@codefromthecrypt
Copy link
Contributor

codefromthecrypt commented Nov 18, 2021

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
Copy link
Contributor Author

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 golang/go#49656

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant