Arithmetic operators in Go are utilized for performing essential mathematical operations. Below are the types of arithmetic operators and their description:
Operator | Name | Description | Example |
---|---|---|---|
+ | Addition | Combines two values into a single sum. | x + y |
- | Subtraction | Reduces one value by another. | x - y |
* | Multiplication | Multiplies two values together. | x * y |
/ | Division | Divides one value by another. | x / y |
% | Modulus | Returns the remainder after division. | x % y |
++ | Increment | Increases the value of a variable by 1. | x++ |
-- | Decrement | Decreases the value of a variable by 1. | x-- |
Example Code
Go
x
package main
import "fmt"
func main() {
x := 100
y := 50
z := 20
// Addition
sum := x + y
fmt.Println("Sum:", sum)
// Subtraction
difference := x - y
fmt.Println("Difference:", difference)
// Multiplication
product := y * z
fmt.Println("Product:", product)
// Division
quotient := x / y
fmt.Println("Quotient:", quotient)
// Modulus
remainder := x % z
fmt.Println("Remainder:", remainder)
// Increment
x++
fmt.Println("Incremented x:", x)
// Decrement
y--
fmt.Println("Decremented y:", y)
}