Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The = operator in Go is the standard assignment operator used to update the value of an existing variable or explicitly discard a value. It evaluates the expression(s) on its right-hand side (RHS) and assigns the resulting value(s) to the corresponding operand(s) on its left-hand side (LHS).
operand_list = expression_list

Technical Characteristics

Valid Left-Hand Side Operands The LHS of an assignment must be an expression list consisting of addressable operands (such as variables, pointer indirections, or struct fields), map index expressions, or the blank identifier (_). Except for the blank identifier, the LHS operand must represent a previously declared or allocated memory location. The Blank Identifier The blank identifier (_) can be used as an LHS operand to discard a value yielded by the RHS. Assignments to the blank identifier do not allocate memory or mutate any existing state. This is strictly an assignment operation and is frequently used to ignore specific return values from multi-value expressions.
var err error
_, err = functionReturningTwoValues() // Discards the first value
Type Strictness Go is a statically typed language. The type of the evaluated RHS expression must be strictly assignable to the declared type of the corresponding LHS operand. Go does not perform implicit type coercion during assignment; attempting to assign a value of a mismatched type results in a compile-time error. Statement, Not an Expression Unlike languages such as C or Java where assignment is an expression that yields a value, assignment in Go is a statement. Because it does not return a value, chained assignments are syntactically invalid and will fail to compile.
var a, b int
a = b = 5 // Compile-time error: syntax error: unexpected =, expecting semicolon or newline
Multiple Assignment and Evaluation Order The = operator supports assigning multiple values simultaneously. The number of operands on the LHS must exactly match the number of evaluated values yielded by the RHS. During multiple assignment, Go guarantees a strict two-phase execution:
  1. Evaluation: The operands of index expressions and pointer indirections on the LHS, along with all expressions on the RHS, are evaluated in the usual order.
  2. Assignment: The actual assignments are carried out in left-to-right order.
Because LHS index expressions and pointer indirections are evaluated before any assignments occur, mutations to variables used in LHS indices during the assignment phase do not affect the target memory location.
var a = []int{1, 2, 3}
var i = 0

// Phase 1: a[i] is evaluated using the old value of i (0). RHS evaluates to 10 and 1.
// Phase 2: a[0] is assigned 10, then i is assigned 1.
a[i], i = 10, 1 

Distinction from :=

The = operator is strictly for assignment, whereas the := operator is for short variable declaration.
  • = updates the value of an existing operand or discards it.
  • := allocates memory for a new variable, infers its type from the RHS, and assigns the initial value.
var count int // Declaration
count = 10    // Assignment using '='

total := 20   // Declaration and initialization using ':='
total = 25    // Subsequent assignment using '='
Master Go with Deep Grasping Methodology!Learn More