Fasten your seatbelts, coder! Today, we're immersing ourselves in Go, mastering arithmetic and logical operations on primitive types. These abilities are vital for data manipulation and decision-making during our coding adventure.
Remember that Go's primitive data types include int
for whole numbers, float64
for decimal numbers, bool
for true/false values, and string
for textual content. Both int
and float64
exhibit constraints in their numerical spans, which we'll explore when we discuss overflow later in this lesson.
We can perform arithmetic operations — addition (+
), subtraction (-
), multiplication (*
), division (/
), and modulus — the remainder of the division (%
) — on numerical types. Here's how we do it:
Go1package main 2import "fmt" 3 4func main() { 5 var a int = 10 6 var b int = 2 7 fmt.Println(a + b) // Outputs: 12 8 fmt.Println(a - b) // Outputs: 8 9 fmt.Println(a * b) // Outputs: 20 10 fmt.Println(a / b) // Outputs: 5 11 fmt.Println(a % b) // Outputs: 0 12}
Go supports alteration of order using parentheses and provides the modulus (%
) operation, handy for determining whether numbers are even or odd!
Logical operators — &&
(AND), ||
(OR), !
(NOT) — function as decision-makers in Go, returning bool
values — true
or false
. Here's how we can utilise them with two bool
variables:
Go1package main 2import "fmt" 3 4func main() { 5 fmt.Println(true && true) // true 6 fmt.Println(true && false) // false 7 fmt.Println(false && true) // false 8 fmt.Println(false && false) // false 9 10 fmt.Println(true || true) // true 11 fmt.Println(true || false) // true 12 fmt.Println(false || true) // true 13 fmt.Println(false || false) // false 14 15 fmt.Println(!true) // false 16 fmt.Println(!false) // true 17}
In this case, &&
yields true
only if both boolean inputs are true
, ||
outputs true
if either of the inputs is true
, and !
reverses the boolean value.
However, the primary use of logical operations is with variables. Let's quickly illustrate the basic usage:
Go1package main 2import "fmt" 3 4func main() { 5 var speed int = 60 6 var minSpeed int = 30 7 var maxSpeed int = 70 8 // Check if the speed is within the accepted range. 9 fmt.Println(speed > minSpeed && speed < maxSpeed); // Prints: true 10}
The concept of overflow explains what happens when we exceed the range allocation of an integer variable. It happens when we attempt to store a value that surpasses the capacity of the variable's type:
Go1package main 2import "fmt" 3import "math" 4 5func main() { 6 var maxInt int = math.MaxInt32 // the maximum integer, equivalent to 2^31 - 1 7 var overflow int = maxInt + 1 // causes an overflow, there's no integer after the maximal one. 8 fmt.Println(overflow) // Prints: -2147483648, which is -2^31 - the minimum integer number 9}
Here, maxInt
is the largest integer value int
can encapsulate. Note that we need to import "math"
to use the math.MaxInt32
variable. When we increment it by one, it 'overflows' to the lowest possible integer value! This reminds us that integer values are "cyclic" in nature.
Well done! Today, we unveiled arithmetic operations, made intricate decisions using logical operators, and became familiar with the effects of overflow. Up next are some practice exercises to reinforce these crucial concepts in Go. Coders, it's time to start hacking!