488

What is the difference between the = and := operators, and what are the use cases for them? They both seem to be for an assignment?

4

8 Answers 8

501

In Go, := is for declaration + assignment, whereas = is for assignment only.

For example, var foo int = 10 is the same as foo := 10.

5
  • 9
    Is there a use case for = as opposed to :=? Should you just always use :=? Jul 27, 2017 at 15:45
  • 13
    @KennethWorden Go won't let you use := to assign to a variable that has already been declared, unless you are assigning to multiple variables at once, and at least one of those variables is new. Aug 30, 2017 at 18:36
  • 15
    the int is not required, var foo = 10 is the same as foo := 10
    – Gary Lyn
    Nov 21, 2017 at 14:04
  • 5
    @KennyWorden, yes. You can't use := outside a function.
    – karuhanga
    Aug 10, 2019 at 20:15
  • 2
    I believe the := is used for type inference as well, you can use it like ` i := 1 ` as opposed to ` var i int = 1 `
    – nulltron
    Feb 12, 2022 at 6:05
327

TLDR:

  • Declare a variable, use: :=.
  • Change a variable’s value, use: =.

There are some rules and the TLDR is not always true.

As others have explained already, := is for both declaration, assignment, and also for redeclaration; and it guesses (infers) the variable's type automatically.

For example, foo := 32 is a short-hand form of:

var foo int
foo = 32

// OR:
var foo int = 32

// OR:
var foo = 32

/ Here are the rules: /

★ 1st Rule:

You can't use := outside of funcs. It's because, outside a func, a statement should start with a keyword.

// no keywords below, illegal.
illegal := 42

// `var` keyword makes this statement legal.
var legal = 42

func foo() {
  alsoLegal := 42
  // reason: it's in a func scope.
}

★ 2nd Rule:

You can't use them twice (in the same scope):

legal := 42
legal := 42 // <-- error

Because, := introduces "a new variable", hence using it twice does not redeclare a second variable, so it's illegal.


★ 3rd Rule:

You can use them for multi-variable declarations and assignments:

foo, bar   := 42, 314
jazz, bazz := 22, 7

★ 4th Rule (Redeclaration):

You can use them twice in "multi-variable" declarations, if one of the variables is new:

foo, bar  := someFunc()
foo, jazz := someFunc()  // <-- jazz is new
baz, foo  := someFunc()  // <-- baz is new

This is legal, because, you're not declaring all the variables, you're just reassigning new values to the existing variables, and declaring new variables at the same time. This is called redeclaration.


★ 5th Rule:

You can use the short declaration to declare a variable in a newer scope even if that variable is already declared with the same name before:

var foo int = 34

func some() {
  // because foo here is scoped to some func
  foo := 42  // <-- legal
  foo = 314  // <-- legal
}

Here, foo := 42 is legal, because, it declares foo in some() func's scope. foo = 314 is legal, because, it just assigns a new value to foo.


★ 6th Rule:

You can declare the same name in short statement blocks like: if, for, switch:

foo := 42
if foo := someFunc(); foo == 314 {
  // foo is scoped to 314 here
  // ...
}
// foo is still 42 here

Because, foo in if foo := ..., only belongs to that if clause and it's in a different scope.


References:

0
280

Only = is the assignment operator.

:= is a part of the syntax of the short variable declaration clause.
👉 There are some rules though. See this other answer for more details.

1
25

:= is a short-hand for declaration.

a := 10
b := "gopher"

a will be declared as an int and initialized with value 10 where as b will be declared as a string and initialized with value gopher.

Their equivalents using = would be

var a = 10
var b = "gopher"

= is assignment operator. It is used the same way you would use it in any other language.

You can omit the type when you declare the variable and an initializer is present (http://tour.golang.org/#11).

1
  • «= is assignment operator. It is used the same way you would use it in any other language.» Except in Ada where = is only for comparison and := is for assignment... Jul 16, 2019 at 6:26
19

The := means declare and assign while the = means to simply assign.

0
12

:= declares and assigns, = just assigns

It's useful when you don't want to fill up your code with type or struct declarations.

// Usage with =
var i int
var U, V, W float64
var k = 0
var x, y float32 = -1, -2

// Usage with :=
i, j := 0, 10
f := func() int { return 7 }
ch := make(chan int)
8

from the reference doc : (tour.golang.org)

Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type.

Outside a function, every construct begins with a keyword (var, func, and so on) and the := construct is not available.

2

In Go := is for declaration and assignment also whereas = is only for the Assignment

for example:

var s string = "Omkar"

s:= "Omkar"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.