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