Arithmetic Operators

Arithmetic Operators in Go #

Arithmetic operators are used to perform common mathematical operations on numbers.

OperatorNameDescriptionExample
+AdditionAdds together two valuesx + y
-SubtractionSubtracts one value from anotherx - y
*MultiplicationMultiplies two valuesx * y
/DivisionDivides one value by anotherx / y
%ModulusReturns the division remainderx % y
++IncrementIncreases the value of a variable by 1x++
--DecrementDecreases the value of a variable by 1x--

Example #

package main
import ("fmt")

func main() {
  x := 10
  y := 3

  fmt.Println("x + y =", x + y)  // 13
  fmt.Println("x - y =", x - y)  // 7
  fmt.Println("x * y =", x * y)  // 30
  fmt.Println("x / y =", x / y)  // 3
  fmt.Println("x % y =", x % y)  // 1

  x++
  fmt.Println("After increment, x =", x) // 11

  y--
  fmt.Println("After decrement, y =", y) // 2
}