Programming involves a lot of numbers. We can use them to represent many things: screen-size dimensions, geographic locations, currencies, colors through numeric codes, etc.
Since we work with numbers so much in programming, learning how to handle mathematical operations will be helpful for a lot of programming tasks.
Operators
Operation symbols and functions are known as operators. An example of an operator is the plus sign indicating addition in math.
Many operators we know from math are present in Go. Other operators, however, will be unique to computer programming.
Let's take a glance at all the operators we will examine in this topic:
x + y // The result is the sum of x and y.
x - y // The result is the difference of x and y.
-x // x will change its sign.
+x // Return identity of x.
x * y // Return the product of x and y.
x / y // Return the quotient of x and y.
x % y // Return the remainder of x / y.Furthermore, we will explore compound assignment operators like += and *= that combine arithmetic operations with the = operator.
Addition and subtraction
In Go, addition and subtraction operators perform just like in math; this means we can use the Go programming language as a calculator.
Let's take a look at some examples, starting with integers:
result := 1 + 5 // result = 6
result2 := 1234 + 4321 // result = 5555Because integer variables can be both positive and negative numbers (and 0 too), we can add a negative number to a positive number:
result := -1000 + 2000 // result = 1000The addition will behave similarly with a float64 variable:
result := 10.1 + 1.234 // result = 11.334
result2 := -5.6789 + 19.23 // result = 13.551100000000002Go will only allow us to use arithmetic operators on the same data types. For example, we can't add an int and a float64.
var x = 8
var y = 13.0
result := x + y // invalid operation: x + y (mismatched types int and float64)
// IDE will warn you, and if you try to compile,
// the Go compiler will print a compile-time errorUnary arithmetic operations
Unary mathematical expressions consist only of one component. In Go, we can use the + and - signs paired with a value to return the value's identity or change the sign of the value. For example:
// Using '+' with a positive value returns the value's identity, a positive value
x := 3.3
y := +x // y = 3.3
// Using '+' with a negative value returns the value's identity, a negative value
x = -30
y = +x // y = -30
// Using '-' with a positive value changes the value to negative
x = 5
y = -x // y = -5
// Using '-' with a negative value changes the value to positive
x = -20
y = -x // y = 20Multiplication, division, and modulo
Go uses the * and / signs to perform multiplication and division, respectively. Let's take a look at multiplication:
x := 42.3
y := 13.1
result := x * y // result = 554.13Division results in Go will vary according to the numeric type of the values. When dividing int values, the result will always be rounded down to the nearest integer; to get the result as a decimal number, we can wrap our values within float32() or float64(). For example:
// Dividing 'int' values
x := 13
y := 2
result := x / y // result = 6
a := -100
b := 3
result2 := a / b // result2 = -33
// Casting 'int' values as 'float64'
x = 20
y = 3
result3 := float64(x) / float64(y) // result3 = 6.666...7Finally, the % sign is the modulo operator. It returns the remainder rather than the quotient of a division and can only be used with the int type values.
As an example, here is a modulo operation:
x := 100
y := 15
result := x % y // result = 10Operator precedence
The order in which operators are evaluated in Go is the same as in mathematics: they are evaluated according to their precedence and from left to right or right to left.
We can look at the following mathematical expression:
result := 20 + 20 * 5 // In this case, multiplication takes place first,
// thus result = 120Just like in mathematics, if we wish to add 20 plus 20 first and then multiply by 5, we use parentheses in Go:
result := (20 + 20) * 5 // In this case,
// the operation within the parentheses takes place first,
// thus result = 200PEMDAS is an acronym that can help you remember the order of operations; it stands for Parentheses first, Exponents second, then Multiplication/Division/Modulo, and Addition/Subtraction last.
To sum up, below is the precedence order of arithmetic operators in Golang, including parentheses. Since Golang does NOT support the exponent operator, it has been omitted from the list below. The list is sorted from the highest to the lowest priority:
Parentheses
Unary plus/minus
Multiplication, division, and modulo
Addition and subtraction
Assignment operators
The assignment operation is performed with the equals = sign. It works by assigning the value on the right to the variable on the left. For example:
var v int
v = 23 // the integer 23 is assigned to the variable vThere are also compound assignment operators that perform an arithmetic operation on a variable's value and then assign the result of the operation to the variable. In straightforward terms, they combine an arithmetic operation with an assignment. Consider the following example:
x := 5
// Compound addition
x += 1 // equivalent to x = x + 1 -> x = 6
// Compound subtraction
x -= 10 // equivalent to x = x - 10 -> x = -4
// Compound multiplication
x *= 4 // equivalent to x = x * 4 -> x = -16
// Compound division
x /= 2 // equivalent to x = x / 2 -> x = -8
// Compound modulo
x %= 3 // equivalent to x = x % 3 -> x = -2 (final result.)Note that when using a compound assignment operator with an arithmetic operation on the right side of the = sign, the arithmetic operation gets prioritized over the compound assignment operator, for example:
y := 6
y *= 3 + 4 // equivalent to y = y * (3 + 4) -> y = 42Increment and decrement operators
The increment and decrement operator in Golang is the statement that increments or decrements the operand to the left of it by 1.
num := 0
num++ // num = 1
num++ // num = 2
num-- // num = 1
num-- // num = 0Despite the convenience of the syntax, there is something we should point out about these two special operators.
They are statements, not expressions; in computer science, a statement never returns a value. Since it cannot be evaluated as a value, we can't pass it to another function as an argument, assign it directly to another variable or combine these operators inside another expression or statement. In short, this means that the increment/decrement operators in Go are as simple as it gets:
num1 := 1
num1++ // Works!
num2 := num1++ // Doesn't work!
num3 := (num1++)++ // Doesn't work!
num4 := 1 + num1++ // Doesn't work!We can see that the only way to implement the increment/decrement operators is to use them alone in one line with the form i++ or i--.
Conclusion
Congratulations! It's been a big topic where we've learned a lot about arithmetic operations in Go, what they are and how to use them. In particular, we've covered the following:
Addition
+and subtraction-;Unary arithmetic operations;
Multiplication
*, division/, and modulo%;Operator precedence;
Assignment operators.
Good luck with using them in your further work!