Selected Reading

Go - Arithmetic Operators



Following table shows all the arithmetic operators supported by Go language. Assume variable A holds 10 and variable B holds 20, then −

Operator Description Example
+ Adds two operands A + B gives 30
- Subtracts second operand from the first A - B gives -10
* Multiplies both operands A * B gives 200
/ Divides the numerator by the denominator. B / A gives 2
% Modulus operator; gives the remainder after an integer division. B % A gives 0
++ Increment operator. It increases the integer value by one. A++ gives 11
-- Decrement operator. It decreases the integer value by one. A-- gives 9

Example - Usage of + and - Operators

Try the following example to understand + and - operators available in Go programming language −

main.go

package main

import "fmt"

func main() {

   var a int = 21
   var b int = 10
   var c int

   c = a + b
   fmt.Printf("a + b is %d\n", c )
   
   c = a - b
   fmt.Printf("a - b is %d\n", c )
}

Output

When you compile and execute the above program, it produces the following result −

a + b is 31
a - b is 11

Example - Usage of * and / Operators

Try the following example to understand * and / operators available in Go programming language −

main.go

package main

import "fmt"

func main() {

   var a int = 21
   var b int = 10
   var c int

   c = a * b
   fmt.Printf("a * b is %d\n", c )
   
   c = a / b
   fmt.Printf("a / b is %d\n", c )
}

Output

When you compile and execute the above program, it produces the following result −

a * b is 210
a / b is 2

Example - Usage of ++ and -- Operators

Try the following example to understand ++ and -- operators available in Go programming language −

main.go

package main

import "fmt"

func main() {

   var a int = 21
   
   a++
   fmt.Printf("a++ is %d\n", a )
   
   a--
   fmt.Printf("a-- is %d\n", a )
}

Output

When you compile and execute the above program, it produces the following result −

a++ is 22
a-- is 21
Tutorix - AI Tutor
Advertisements