Greetings! Today, we're venturing into the rewarding realm of Go comparison operators. These operators are of paramount importance as they control the flow of code by enabling value comparisons.
Our goal for this session is to deepen our understanding of the usage of comparison operators in Go programs. We will investigate various Go comparison operators and reinforce your learning through real-world examples.
Imagine operating a submarine in the deep sea. Here, you determine your routes by evaluating conditions such as the distances to underwater landmarks. These decisions boil down to comparisons, similar to situations encountered in programming. In Go, we use comparison operators to facilitate such rational decision-making.
The Go programming language includes six comparison operators: equal to (==
), not equal to (!=
), greater than (>
), less than(<
), greater than or equal to (>=
), and less than or equal to (<=
). These operators return either true
or false
, also known as Boolean values.
Consider this comparison of a submarine's speed in relation to an ocean current as an example:
Go1var submarineSpeed = 30 // speed in knots 2var currentSpeed = 20 // speed in knots 3fmt.Println("Is the submarine faster than the ocean current? ", submarineSpeed > currentSpeed) 4// Prints: Is the submarine faster than the ocean current? true
In the above code, we used the >
operator to compare the submarineSpeed
and the currentSpeed
. The result is true
because the submarineSpeed
is greater than the currentSpeed
.
Now, let's delve into the equal to (==
) and not equal to (!=
) operators. These become crucial when you need to compare values, such as when comparing the current oxygen level to the desired one:
Go1var currentOxygenLevel = 70 // current oxygen level in % 2var requiredOxygenLevel = 100 // required oxygen level in % 3 4var isOxygenEnough = currentOxygenLevel == requiredOxygenLevel // this results in 'false' 5var isOxygenLow = currentOxygenLevel != requiredOxygenLevel // this results in 'true'
The ==
operator checks whether the currentOxygenLevel
equals the requiredOxygenLevel
, yielding a false
result. Conversely, the !=
operator verifies their inequality, returning true
.
Next, let's examine the less than (<
), greater than (>
), less than or equal to (<=
), and greater than or equal to (>=
) operators. These operators are primarily used for numeric data comparisons. Suppose you're surveying two underwater caves and want to determine which one is closer. You can utilize these operators to make an informed decision:
Go1var distanceToCaveA = 2000 // distance in meters 2var distanceToCaveB = 1000 // distance in meters 3 4var isACloser = distanceToCaveA < distanceToCaveB // this results in 'false' 5var isBCloserOrSame = distanceToCaveA >= distanceToCaveB // this results in 'true'
Here, we compare the distances to two underwater caves. The submarine is not closer to cave A, so isACloser
is false
. However, the submarine is closer to, or at the same distance from, cave B —isBCloserOrSame
is true
.
Excellent work! We've embarked on an exploration of the Go comparison operators and their practical applications. Armed with this knowledge, you can now make decisions and effectively control the flow of your Go programs. Coming up next, we will dive deeper with hands-on practice exercises. So strap in, and let's dive in!