2.4 Assignments
The value held by a variable is updated by an assignment statement,
which in its simplest form has a variable on the left of the =
sign and an expression on the right.
x = 1 // named variable *p = true // indirect variable person.name = "bob" // struct field count[x] = count[x] * scale // array or slice or map element
Each of the arithmetic and bitwise binary operators has a corresponding assignment operator allowing, for example, the last statement to be rewritten as
count[x] *= scale
which saves us from having to repeat (and re-evaluate) the expression for the variable.
Numeric variables can also be incremented and
decremented by ++
and --
statements:
Get The Go Programming Language now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.