In go there are functions which return two values or more values, commonly one is an error. Suppose that I want to store the first return value into an already initialized variable, but I would like to initialize the variable to contain the error inline. Is there a way to do this?
For example, say I had this code
var a int //This code doesn't compile because err doesn't exist a, err = SomeFuncWithTwoReturnValues() //This code doesn't compile either a, err := SomeFuncWithTwoReturnValues()
I know you could do this, but I was hoping there was a way to do it all inline
var a int var err error a, err = SomeFuncWithTwoReturnValues()
or
a, err := SomeFuncWithTwoReturnValues()
EDIT: The code above actually compiles, so I looked back at my code to drill down more and have created a quick sample that actually replicates the problem (not just in my mind...).
package main func myfunc() (int, int) { return 1, 1 } func main() { a := make([]int, 1) a[0], b := myfunc() a[0] = b }
Compiler says main.go|9| non-name a[0] on left side of :=
. If I make it =
instead of :=
though then b is never created. I get the feeling that there is not shorthand way to do it though.
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.
You can return multiple values from a function using either a dictionary, a tuple, or a list. These data types all let you store multiple values.
You can assign the same value to multiple variables by using = consecutively. This is useful, for example, when initializing multiple variables to the same value. It is also possible to assign another value into one after assigning the same value.
As you've mentioned in the comments, you'll need to use the =
operator in order to assign to a variable you've already declared. The :=
operator is used to simultaneously declare and assign a variable. The two are the same:
var x int x = 5 //is the same as x := 5
This solution will at least compile:
package main func myfunc() (int, int) { return 1, 1 } func main() { var b int a := make([]int, 1) a[0], b = myfunc() a[0] = b }
To answer your question, I don't think there is a way to simultaneously use an undeclared and a declared variable when returning multiple values. That would be trying to use two different operators simultaneously.
Edit: just saw your example from the code that compiles, so it appears you're already familiar with go's assignment operators. I'll leave the example up anyway.
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