Taking single value from multi-return function and inlining function calls

331 views
Skip to first unread message

Tanmay Das

unread,
Aug 29, 2022, 4:42:49 AM8/29/22
to golang-nuts
Hi,

I am relatively new to the Go language, and I have always wanted to ask this question:

Consider the following snippet:

package main

import "fmt"

func returnMany() (int, int) {
    return 4, 2
}

func useOne(value int) {
    fmt.Println(value)
}

func main() {
    useOne(returnMany())
}

The returnMany function returns multiple values and the useOne function accepts only a single value. I have often come across situations where the returnMany() resides in my codebase and the useOne() comes from an external library.

The code above obviously fails to build. Typically as a consumer of "useOne" package, I know which value it expects and I can either do this:

value, _ := returnMany()
useOne(value)

Or, this, depending on my need:

_, value := returnMany()
useOne(value)

This makes inlining function calls impossible and adds a little bit of noise to the codebase, IMO.

Is there a way I can inline this without introducing a temp variable? Pseudocode example:

useOne(returnMany()[1]) // 4
useOne(returnMany()[2]) // 2

https://go.dev/play/p/gxKsnRKUAFY

Axel Wagner

unread,
Aug 29, 2022, 4:59:21 AM8/29/22
to Tanmay Das, golang-nuts
You could write helpers:

func Left[A, B any](a A, b B) A { return a }
func Right[A, B any](a A, b B) B { return b }

Which you can then insert

useOne(Left(returnMany())
useOne(Right(returnMany())

But TBQH, I would not consider this good style. It ultimately adds more noise and overhead, as people now have to figure out what Left and Right do (you might argue that they should be named better, but still).

The right thing to do here, in my opinion, is to just accept the extra statement. Yes, it's a little bit more noisy. But it's not a dramatic enough different to justify deviating from what the core language gives you.

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/80fbcec2-0ee2-4cde-ac31-de495cb97c20n%40googlegroups.com.

Aaron Rubesh

unread,
Aug 29, 2022, 8:19:27 AM8/29/22
to golang-nuts
Another option for this is if your function is always called after the call to the external library, you can change your function signature to match the return value of the external library. 

Its quite common to see something like this:

`
func A() (int, error) {} 

func UseA(a int, err error) (int, error) {
  if err != nil {return err} 
  ....
}

... 

UseA(A()) 
`


From: 'Axel Wagner' via golang-nuts <golan...@googlegroups.com>
Sent: Monday, August 29, 2022 3:58:38 AM
To: Tanmay Das <tanma...@gmail.com>
Cc: golang-nuts <golan...@googlegroups.com>
Subject: Re: [go-nuts] Taking single value from multi-return function and inlining function calls
 
Reply all
Reply to author
Forward
0 new messages