Arithmetic Operators in Go #
Arithmetic operators are used to perform common mathematical operations on numbers.
Operator | Name | Description | Example |
---|---|---|---|
+ | Addition | Adds together two values | x + y |
- | Subtraction | Subtracts one value from another | x - y |
* | Multiplication | Multiplies two values | x * y |
/ | Division | Divides one value by another | x / y |
% | Modulus | Returns the division remainder | x % y |
++ | Increment | Increases the value of a variable by 1 | x++ |
-- | Decrement | Decreases the value of a variable by 1 | x-- |
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
}