Operator and syntax

Go compound assignment operators

Combines a binary operation with assignment. The left operand is evaluated once; the underlying operation must be valid for its type. Example output: '10'.

Formal Syntax & Structure

go syntaxgo
+= -= *= /= %= &= |= ^= &^= <<= >>=

Working Example

Example Snippetgo
package main
import (  "fmt")
func main() {  x := 12  x += 4  x -= 2  x *= 2  x /= 2  x %= 5  x |= 8  x &= 15  x ^= 1  x &^= 8  x <<= 2  x >>= 1  fmt.Println(x)}
Go Reference