no new variables on left side of :=
Asked Answered
K

4

132

What's happening here?

package main

import "fmt"

func main() {

    myArray  :=[...]int{12,14,26}  ;     
    fmt.Println(myArray)

    myArray  :=[...]int{11,12,14} //error pointing on this line 

    fmt.Println(myArray) ;

}

It throws an error that says

no new variables on left side of :=

What I was doing was re-assigning values to an already declared variable.

Klarrisa answered 11/11, 2012 at 6:42 Comment(1)
try myArray,x = [...]int{11,12,14},3Sordino
E
174

Remove the colon : from the second statement as you are assigning a new value to existing variable.

myArray = [...]int{11,12,14}

colon : is used when you perform the short declaration and assignment for the first time as you are doing in your first statement i.e. myArray :=[...]int{12,14,26}.

Equitant answered 11/11, 2012 at 6:48 Comment(0)
G
33

There are two types of assignment operators in go := and =. They are semantically equivalent (with respect to assignment) but the first one is also a "short variable declaration" ( http://golang.org/ref/spec#Short_variable_declarations ) which means that in the left we need to have at least a new variable declaration for it to be correct.

You can change the second to a simple assignment statement := -> = or you can use a new variable if that's ok with your algorithm.

Golding answered 11/11, 2012 at 7:24 Comment(1)
And := can't be used with _? e.g. _ := someFunc()Mullin
D
11

As a side note, redeclaration can only appear in a multi-variable short declaration

Quoting from the Language specification:

Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration. Redeclaration does not introduce a new variable; it just assigns a new value to the original.

package main

import "fmt"


func main() {
    a, b := 1, 2
    c, b := 3, 4

    fmt.Println(a, b, c)
}

Here is a very good example about redeclaration of variables in golang: https://mcmap.net/q/174981/-why-there-are-two-ways-of-declaring-variables-in-go-what-39-s-the-difference-and-which-to-use

Dodwell answered 13/6, 2018 at 5:30 Comment(1)
Thanks for the note. I've confused by a multi-variable short declaration, this explains why.Lynxeyed
B
8
myArray  :=[...]int{12,14,26}

As stated by the previous commenters, := is a type of short-hand and/or the short-form of variable declaration.

So in the statment above you are doing two things.

  1. You are declaring your variable to be myArray.
  2. You are assigning an array of integers to the myArray variable.

The second part of your code fails, because what you are doing here:

myArray  :=[...]int{11,12,14} //error pointing on this line 

Is RE-declaring the existing variable myArray, which already contains integer values.

This works:

myArray = [...]int{11,12,14} // NO error will be produced by this line

Because, it is assigning the integer array to the existing ( pre-declared / initialized ) variable.

Boner answered 13/11, 2012 at 0:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.