Go Assignment Operators

Introduction

Go assignment operators can assign values to variables.

The following table lists the Go assignment operators:

Assignment DescriptionExample
x = y Assign x = y
x += y Add and assign x = x + y
x -= y Subtract and assignx = x - y
x *= y Multiply and assignx = x * y
x /= y Divide and assign quotient x = x / y
x %= y Divide and assign modulus x = x % y

The following example shows how to use Go assignment operators.

package main /* w w  w  . ja va  2s . co m*/

import "fmt"

func main() {
  var x, y = 15, 25
  x = y
  fmt.Println("= ", x)

  x = 15
  x += y
  fmt.Println("+=", x)

  x = 50
  x -= y
  fmt.Println("-=", x)

  x = 2
  x *= y
  fmt.Println("*=", x)

  x = 100
  x /= y
  fmt.Println("/=", x)

  x = 40
  x %= y
  fmt.Println("%=", x)
}



PreviousNext

Related