Live data from Hacker News

Benefits of named return values in Go

blog.minio.io

1–10 of 146 posts

Re: Benefits of named return values in Go

#6
I normally hate it when people immediately trot out the old "premature optimization" quote, but it really applies here.

Please don't go around naming all your returns just because today's compiler happens to generate better code with them. This is a compiler issue that I'm confident will be fixed one day, especially if you do the right thing and file an issue.

But by all means, if you're profiling and your inner loops are actually slowed down by this, then make the change. And add a comment so that someone might be able to change it back some day when the compiler's improved.

Re: Benefits of named return values in Go

#7
I was going to post "But JS has this:

    return { named_var_1, named_var_2 }

    const { named_var_1, named_var_2 } = f()
But then I read the article.

I don't know really what is going on in this article on a quick reading, but it builds by existing sense that go is a really cool language that I ought to get into.

I have no idea if anyone wants to help me understand, but in the last part where he changes to use a named return parameter, he says, you may also "enjoy the cleaner look of the source code, too". To me the only difference I see is the return name, to the right of the existing function signature. What was I missing that makes this actually cleaner?

Re: Benefits of named return values in Go

#10

I was going to post "But JS has this: return { named_var_1, named_var_2 } const { named_var_1, named_var_2 } = f() But then I read the article. I don't know really what is going on in this article on a quick reading, but it builds by existing sense that go is a really cool language that I ought to get into. I have no idea if anyone wants to help me understand, but in the last part where he changes to use a named retu…

It allows you to assign to the "return variables" inside the function, then just return without naming all the returned values at every return.

e.g. https://play.golang.org/p/gdac5QR-wW

This can also be used to return the default value for a type by never assigning anything to the return value.

Post reply on HN