Go Arithmetic Operators

Introduction

Go arithmetic operators can perform common arithmetical operations, such as addition, subtraction, multiplication etc.

Here's a complete list of Golang's arithmetic operators:

Operator DescriptionExample Result
+Addition x + y Sum of x and y
-Subtractionx - y Subtracts one value from another
*Multiplication x * y Multiplies two values
/Division x / y Quotient of x and y
%Modulusx % y Remainder of x divided by y
++ Increment x++ Increases the value of a variable by 1
-- Decrement x-- Decreases the value of a variable by 1

The following example shows how to use these arithmetic operators:

package main /*from   ww w  .jav a 2s.co m*/

import "fmt"

func main() {
  var x, y = 30, 8

  fmt.Printf("x + y = %d\n", x+y)
  fmt.Printf("x - y = %d\n", x-y)
  fmt.Printf("x * y = %d\n", x*y)
  fmt.Printf("x / y = %d\n", x/y)
  fmt.Printf("x mod y = %d\n", x%y)

  x++
  fmt.Printf("x++ = %d\n", x)

  y--
  fmt.Printf("y-- = %d\n", y)
}



PreviousNext

Related