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

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)