Go Assignment Operators
Assignment Operators
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=
) to assign the value 10 to a variable called x:
The addition assignment operator (+=
) adds a value to a variable:
Example
package main
import ("fmt")
func main() {
var x = 10
x +=5
fmt.Println(x)
}
Try it Yourself »
A list of all assignment operators:
Operator | Example | Same As | Try it |
---|---|---|---|
= | x = 5 | x = 5 | Try it » |
+= | x += 3 | x = x + 3 | Try it » |
-= | x -= 3 | x = x - 3 | Try it » |
*= | x *= 3 | x = x * 3 | Try it » |
/= | x /= 3 | x = x / 3 | Try it » |
%= | x %= 3 | x = x % 3 | Try it » |
&= | x &= 3 | x = x & 3 | Try it » |
|= | x |= 3 | x = x | 3 | Try it » |
^= | x ^= 3 | x = x ^ 3 | Try it » |
>>= | x >>= 3 | x = x >> 3 | Try it » |
<<= | x <<= 3 | x = x << 3 | Try it » |
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.