:= and = in Go: What's the Difference? Full Breakdown with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Confusion Between := and = Can Break Your Go Code

\n\n

You're writing in Go, everything is going great, but suddenly the compiler throws an error no new variables on left side of :=. Sound familiar? Or you see a colleague's code where they use both = and :=, and it seems like there's no difference. In reality, the difference is fundamental, and misunderstanding this is one of the most common mistakes for beginners in Golang.

\n\n

In this guide, we'll break down how the operators := (short variable declaration) and = (assignment) differ. You'll learn when to use which, how to avoid errors, and how to write cleaner, more idiomatic Go code. Let's go!

\n\n

Basics: What Are Variables and Why Do We Need Them?

\n\n

Before diving into the depths of operators, let's remember that a variable is a named memory location that stores a value of a specific type. In Go, as in any strongly typed language, you need to handle variables carefully.

\n\n

The operators := and = are two ways to interact with variables, but they solve different tasks. The first one declares a variable, the second one changes its value.

\n\n

The = Operator (Assignment): Only Change, No Declaration

\n\n

The = operator is a classic assignment. It takes an existing variable and writes a new value into it. If the variable doesn't exist, the compiler will throw an error.

\n\n
package main\n\nimport "fmt"\n\nfunc main() {\n    var name string  // explicitly declared the variable\n    name = "Go"      // assigned a value (works)\n    fmt.Println(name)\n\n    // age = 10       // ERROR: variable age not declared\n}\n
\n\n

Here, name was declared using var, so we can use =. If we tried to write age = 10 without prior declaration, the compiler would refuse to compile the code.

\n\n

When to use =?

\n
    \n
  • When the variable is already declared (via var or :=).
  • \n
  • In loops and iterations for updating counters.
  • \n
  • When working with pointers and structures where you need to change a field.
  • \n
\n\n

The := Operator (Short Declaration): Declaration + Assignment in One Step

\n\n

The := operator is "syntactic sugar" that simultaneously declares a variable and assigns it a value. The variable's type is automatically inferred from the expression on the right. This is much shorter than writing var x int = 5.

\n\n
package main\n\nimport "fmt"\n\nfunc main() {\n    name := "Go"   // declared and assigned, type string\n    age := 10      // declared and assigned, type int\n    fmt.Println(name, age)\n}\n
\n\n

This is equivalent to:

\n
var name string = "Go"\nvar age int = 10\n
\n\n

But := is shorter and easier to read. The main rule: there must be at least one new variable on the left side of :=. If all variables are already declared, you will get an error.

\n\n

When to use :=?

\n
    \n
  • For declaring local variables inside functions.
  • \n
  • In for, if, and switch loops with short initialization.
  • \n
  • When working with functions that return multiple values (e.g., err := someFunc()).
  • \n
\n\n

Key Difference: Declaration vs Change

\n\n

The most important difference to remember:

\n
    \n
  • := is a declaration. It creates a new variable in the current scope.
  • \n
  • = is an assignment. It changes an existing variable.
  • \n
\n\n

Think of it as the difference between "creating" a variable and "changing its name." You cannot change a

Blogs

Book Recommendations