Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go: Assign multiple return value function to new and old variable

Tags:

go

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.

like image 583
EntilZha Avatar asked Aug 01 '14 15:08

EntilZha


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.

Can you have multiple return values?

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.

How do you assign multiple values to one variable?

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.


1 Answers

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.

like image 115
E. Moffat Avatar answered Oct 04 '22 10:10

E. Moffat