Arithmetic Operators in Go Programming Language
The arithmetic operators are used to perform common arithmetical operations, such as addition, subtraction, multiplication etc.
Here's a complete list of Golang's arithmetic operators:
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | x + y | Sum of x and y |
- | Subtraction | x - y | Subtracts one value from another |
* | Multiplication | x * y | Multiplies two values |
/ | Division | x / y | Quotient of x and y |
% | Modulus | x % 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 will show you these arithmetic operators in action:
Example
package main
import "fmt"
func main() {
var x, y = 35, 7
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)
}
Output
x + y = 42
x - y = 28
x * y = 245
x / y = 5
x mod y = 0
x++ = 36
y-- = 6
Most Helpful This Week
Anonymous Functions in Golang
Golang Functions Returning Multiple Values
Creating a Function in Golang
Simple function with return value in Golang
Closures Functions in Golang
The return values of a function can be named in Golang
Golang Passing Address to a Function
What is Function in Golang
Naming Conventions for Golang Functions
Higher Order Functions in Golang
Simple function with parameters in Golang