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)
}

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)
}

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

 

 

 

 

 

Login
ADS CODE