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 { ... }
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.
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.
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.
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..... }
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.
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.
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
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...
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With