r/golang•
code_nomad

Adding multiple returns to existing variables

When dealing with a single return, it's easy to do: variable += function(), but it doesn't seem possible to do something like var1+=, var2+= = multiReturn() without creating temporary variables and then doing the variable += tempVar on its own line.

Is there a shorter way to do this?

Posted Mar 4, 2016, 2:11 AM UTC

03
rimpy13•

Unfortunately, there's not. Is this something that comes up a lot? One of Go's design goals was to keep language size down, and this sometimes comes at the cost of an extra line or two of code, especially in less common scenarios like the one you're describing.

2
code_nomadOP•

Not a lot no, but with how concise go lets you be other places, := ,defer instead of using()-style blocks, channels instead of mutex nightmare, etc, it feels odd to make throwaway variables.

1
nicerobot•

If it was going to be something you needed frequently, one approach I tend to use is simply provide a function that takes the same parameter as the multi-return https://play.golang.org/p/igDQw-0dmK

1