BEST SITE FOR WEB DEVELOPERS
Golang. Lessons for beginners

Ua

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:

Example

package main
import ("fmt")

func main() {
  var x = 10
  fmt.Println(x)
}
Try it Yourself »

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 »