3

In calling a library function with the following signature:

func New() (*sql.DB, Sqlmock, error)

like this:

suite.db, suite.mock, err := sqlmock.New() // inside a suite method

I get error

expected identifier on left side of :=

However, when I change to this

var err error
suite.db, suite.mock, err = sqlmock.New()

the error disappears! Why does declaring k < n variables in a := assignment fail?!

Mike Warren
  • 3,796
  • 5
  • 47
  • 99
  • 5
    `:=` is the shortcut operator to declare and initialize a variable at the same time. You cannot "declare" a field of a struct. – Adrian Aug 29 '18 at 19:24

2 Answers2

9

:= is not assignment, it is a short variable declaration. Assignment uses e.g. the simple = operator.

As its name says: it is to declare variables. suite.db is not a variable, it is an expression, more specifically a primary expression; a selector to be exact.

The short variable declaration uses the syntax:

ShortVarDecl = IdentifierList ":=" ExpressionList .

Where IdentifierList is:

IdentifierList = identifier { "," identifier } .

So you must list identifiers. One "exception" to this "declare new variables rule" is the redeclaration:

Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block (or the parameter lists if the block is the function body) 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.

So you are allowed to use existing variables in a short variable declaration if they were declared in the same block, and you also provide new identifiers (not just existing ones–in which case you would have to use assignment instead).

See related: Why there are two ways of declaring variables in Go, what's the difference and which to use?

When you declare err prior and change := to = it works, because the assignment does not require identifiers on the left of the assignment operator, but expressions:

Assignment = ExpressionList assign_op ExpressionList .

And as detailed above, suite.db is an expression, just like existing variables (identifiers).

icza
  • 389,944
  • 63
  • 907
  • 827
4

:= declares a new variable "identifier". This means you are adding a named thing in your program that can be assigned a value. A struct's fields already are named, as in the golang parser knows they exist so using := makes no sense to golang in this case.

Why does declaring k < n variables in a := assignment fail?!

I'm not sure what "k < n" means, but I think you mean "why does having multiple variables on the left side of := fail?". If that's what you mean, that's not true.

x, y, z := func() (int,int,int) {return 1,2,3}()
fmt.Println(x, y, z)

works just fine. The issue is that golang cannot create an "identifier" (aka a newly named variable) for a struct field since that struct field already exists.

EDIT: I just had a brainwave that you might have mean "why does having only some new identifiers to declare on the left side of := not work?". This also is not true.

x, y := 5, 6
fmt.Println(x, y)
x, y, z := 1, 2, 3
fmt.Println(x, y, z)

The above works just fine as well.

Frank Bryce
  • 8,076
  • 4
  • 38
  • 56