Home »
Golang
Golang Arithmetic Operators
By IncludeHelp Last updated : October 05, 2024
Go Arithmetic Operators
Arithmetic or Mathematical operators are used to perform mathematic operations such as addition, subtraction, multiplication, etc.
List of Golang Arithmetic Operators
Operator |
Description |
Example x:=5 y:=2 |
Addition (+) |
Adds two operands |
x+y returns 7 |
Subtraction (-) |
Subtracts two operands (subtracts the seconds operand from the first operand) |
x-y returns 3 |
Multiplication (*) |
Multiplies two operands |
x*y returns 10 |
Division (/) |
Divides the first operand with the second operand and returns the result |
x/y returns 2 |
Modulus (%) |
Returns the remainder of two operands |
x%y returns 1 |
Increment (++) |
Increases the value of the variable by one |
x++ returns 6 |
Decrement (--) |
Decreases the value of the variable by one |
x-- returns 4 |
Example of Golang Arithmetic Operators
The below Golang program is demonstrating the example of arithmetic operators.
// Golang program demonstrate the
// example of arithmetic operators
package main
import "fmt"
func main() {
x := 5
y := 2
result := 0
result = x + y
fmt.Println(x, "+", y, "=", result)
result = x - y
fmt.Println(x, "-", y, "=", result)
result = x * y
fmt.Println(x, "*", y, "=", result)
result = x / y
fmt.Println(x, "/", y, "=", result)
result = x % y
fmt.Println(x, "%", y, "=", result)
x++
fmt.Println("x++ =", x)
y--
fmt.Println("y-- =", y)
}
Output:
5 + 2 = 7
5 - 2 = 3
5 * 2 = 10
5 / 2 = 2
5 % 2 = 1
x++ = 6
y-- = 1