Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang: Use one value in conditional from function returning multiple arguments

Tags:

go

Suppose in Go we have a function returning two arguments

func squareAndCube(int side) (square int, cube int) {     square = side * side     cube = square * side     return } 

Then you would like to use the first (second) value of this function in the conditional:

square, _ := squareAndCube(n) if square > m {     ... } 

However, can we do first two lines in one line if we do not need the value square to use anywhere else? E.g.

 if squareAndCube(n).First() > m {      ...  } 
like image 619
david Avatar asked Jul 23 '14 13:07

david


People also ask

Is it possible for a function to return multiple values in Golang?

In Golang, we can return multiple values at a time from a single function. Multiple return values can be achieved by changing the return type of the function in the function signature. The (int, int) in this function signature explains that the return type is two integers.

How do I return more than one value in go?

In Go language, you are allowed to return multiple values from a function, using the return statement. Or in other words, in function, a single return statement can return multiple values. The type of the return values is similar to the type of the parameter defined in the parameter list.

How many values a Go function can return?

Functions in Golang can return multiple values, which is a helpful feature in many practical scenarios. This example declares a function with two return values and calls it from a main function.

How do you return a function in Golang?

To declare the named result or return parameters, just use the return type part of the function signature. Below is the general syntax to declare a function in Golang. Syntax to declare a function without named return arguments: func function_name(Parameter-list)(Return_type){ // function body..... }

How to return multiple values from a function in Golang?

In Go language, you are allowed to return multiple values from a function, using the return statement. Or in other words, in function, a single return statement can return multiple values. The type of the return values is similar to the type of the parameter defined in the parameter list.

What is return_type_list in Golang?

return_type_list: It is optional and it contains the types of the values that function returns. If you are using return_type in your function, then it is necessary to use a return statement in your function. In Go language, you are allowed to provide names to the return values.

How to combine if/else conditions in Golang?

Often refereed to as Golang because of the domain name, this language also has the If/else conditions. Usually the If/else/else if condition when written with one condition makes the program lengthy and increases the complexity thus we can combine two conditions. The conditions can be combined in the following ways :-Using &&(AND) Operator

Can a function return more than one value?

Or in other words, in function, a single return statement can return multiple values. The type of the return values is similar to the type of the parameter defined in the parameter list. func function_name (parameter_list) (return_type_list) { // code...


2 Answers

You cannot pick one of the multiple returned values but you can write something like

if square, _ := squareAndCube(n); square > m {     // ... } 

The square variable will only be valid in the if scope. These "simple statements" can be used in if statements, switch statements and other constructs such as for loops.

See also the effective go article on if statements.

like image 179
nemo Avatar answered Sep 24 '22 06:09

nemo


Found this blog post by Vladimir Vivien that has a nice workaround for the problem. The solution is to create a function that "...exploits the automatic conversion from the compiler to convert a vararg parameters in the form of "x...interface{}" to a standard []interface{}."

func mu(a ...interface{}) []interface{} {     return a } 

Now you can wrap any function with multiple returned values in mu and index the returned slice followed by a type assertion

package main  import(     "fmt" )  func mu(a ...interface{}) []interface{} {     return a }  func myFunc(a,b string) (string, string){     return b, a }  func main(){     fmt.Println(mu(myFunc("Hello", "World"))[1].(string)) }  // output: Hello 

EDIT: See comment by Matt Mc

like image 45
Eric Egan Avatar answered Sep 23 '22 06:09

Eric Egan