Can you declare multiple variables at once in Go?

GoVariable Assignment

Go Problem Overview


Is it possible to declare multiple variables at once using Golang?

For example in Python you can type this:

a = b = c = 80

and all values will be 80.

Go Solutions


Solution 1 - Go

Yes, you can:

var a, b, c string
a = "foo"
fmt.Println(a)

You can do something sort of similar for inline assignment, but not quite as convenient:

a, b, c := 80, 80, 80

Solution 2 - Go

In terms of language specification, this is because the variables are defined with:

VarDecl     = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) .
VarSpec     = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) .

(From "Variable declaration")

A list of identifiers for one type, assigned to one expression or ExpressionList.

const a, b, c = 3, 4, "foo"  // a = 3, b = 4, c = "foo", untyped integer and string constants
const u, v float32 = 0, 3    // u = 0.0, v = 3.0

Solution 3 - Go

Another way to do this is like this

var (
   a = 12
   b = 3
   enableFeatureA = false

   foo = "bar"
   myvar float64
   anothervar float64 = 2.4
)

Also works for const

const (
  xconst    = 5
  boolconst = false
)

Solution 4 - Go

Yes you can and it is slightly more nuanced than it seems.

To start with, you can do something as plain as:

var a, b, x, y int  // declares four variables all of type int

You can use the same syntax in function parameter declarations:

func foo(a, b string) {  // takes two string parameters a and b
    ...
}

Then comes the short-hand syntax for declaring and assigning a variable at the same time.

x, y := "Hello", 10   // x is an instance of `string`, y is of type `int`

An oft-encountered pattern in Golang is:

result, err := some_api(...)  // declares and sets `result` and `err`
if err != nil {
    // ...
    return err
}

result1, err := some_other_api(...)   // declares and sets `result1`, reassigns `err`
if err != nil {
    return err
}

So you can assign to already-defined variables on the left side of the := operator, so long as at least one of the variables being assigned to is new. Otherwise it's not well-formed. This is nifty because it allows us to reuse the same error variable for multiple API calls, instead of having to define a new one for each API call. But guard against inadvertent use of the following:

result, err := some_api(...)  // declares and sets `result` and `err`
if err != nil {
    // ...
    return err
}

if result1, err := some_other_api(...); err != nil {   // result1, err are both created afresh, 
                                                       // visible only in the scope of this block.
                                                       // this err shadows err from outer block
    return err
}

Solution 5 - Go

Several answers are incorrect: they ignore the fact that the OP is asking whether it is possible to set several variables to the same value in one go (sorry for the pun).

In go, it seems you cannot if a, b, c are variables, ie you will have to set each variable individually:

a, b, c := 80, 80, 80

But if a, b, c are constants, you can:

const (
		a = 80
		b
		c
	)

Solution 6 - Go

Try this in the go-playground: https://play.golang.org/

package main

import "fmt"

func main() {
	a, b := "a", "b"; //Declare And Assign
	var c, d string;  //Declare Only
	fmt.Println(a,b);
	fmt.Println(c,d);
}

Solution 7 - Go

Another way of doing is using var for package level assignment

package main

import (
	"fmt"
)

var (
	a, b, c = 80, 80 ,80
)

func main() {
	fmt.Println(a, b, c)
}

Solution 8 - Go

long declaration

var varName1, varName2 string = "value","value"

short declaration

varName1,varName2 := "value1","value2"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKevin BurkeView Question on Stackoverflow
Solution 1 - GoKevin BurkeView Answer on Stackoverflow
Solution 2 - GoVonCView Answer on Stackoverflow
Solution 3 - GohookenzView Answer on Stackoverflow
Solution 4 - GoCppNoobView Answer on Stackoverflow
Solution 5 - GoOliverView Answer on Stackoverflow
Solution 6 - GoKANJICODERView Answer on Stackoverflow
Solution 7 - GoSaravanaKumar KKBView Answer on Stackoverflow
Solution 8 - GoAmit KumarView Answer on Stackoverflow