Welcome Gophers! In this video, we are going to be looking at if/else
statements within the Go programming language!
Example
Imagine we were building a tool that automatically approved or rejected a customer for a fairground ride depending on their height. We could start off
by defining a customerHeight
int value to store the height in centimeters.
var customerHeight int = 140
We could then use an if/else
statement in order to decide whether or not this customer can go on the adult rides:
if customerHeight >= 150 {
println("customer can access ride")
} else {
println("customer is not tall enough")
}
In the above example, we’ve provided a condition which is evalutated for it’s truthiness. In other words, does this expression return a true value? If it does, then we want to execute the code contained within the following brackets.
However, if the truthiness of this statement is false, then we revert to executing the else
clause of our if/else statement. In this case, we should see customer is not tall enough
printed out as the expression customerHeight >= 150
evaluates to false
.
Multiple If Statements
Let’s have a look at how we could extend our if/else statements with further if conditionals. Let’s say that we wanted to add a check to see if the customer can access children’s rides based on their height. We could use an else if
clause in order to evaluate the truthiness of another condition and then execute a block of code if the condition returns true.
if customerHeight >= 150 {
println("customer can access ride")
} else if customerHeight >= 120 {
println("customer can access children's rides")
} else {
println("customer is not tall enough")
}
In the above code, the first condition customerHeight >= 150
evaluates to false as our customerHeight is currently set to 140. When this is evaluated as false, we then move to the next else if
block and evaluate the next condition.
In this case, customerHeight >= 120
returns true and the code within this block is then executed.
If we have additional checks we want to perform, we can chain them before our final else
block.
Multiple Conditions
Let’s say we wanted to introduce another check to see if the customer is old enough to access the adult rides. We would have another variable storing the customer’s age:
customerAge := 18
We can then add an additional check in our if statements that will also check that the customerAge is over 18. If they are over 18, then it doesn’t matter what height they are, they can still access the rides:
var customerHeight int = 140
customerAge := 18
if customerHeight >= 150 || customerAge >= 18 {
println("can access ride")
} else if customerHeight >= 120 {
println("customer can access children's rides")
} else {
println("customer is too small")
}
In this example, we’ve used the ||
to represent an OR condition. When our if condition is evaluated, it then checks if customerHeight >= 150
which evalutes to false, it then sees this ||
syntax and evaluates the next condition which is customerAge >= 18
. This then evaluates to true and this if condition is satisfied so the can access ride
message is printed out.
If we wanted to ensure that the customer is over 18 and is also over the 150cm height we could then swap out the ||
OR operator for a &&
AND operator. In this case, both expressions will have to evaluate as true and it will evaluate the conditions in order.
Full Source Code For Lesson:
The full source code for this lesson is below if you’d like to try it out locally!
package main
func main() {
println("If Statements in Go")
var customerHeight int = 140
customerAge := 18
if customerHeight >= 150 || customerAge >= 18 {
println("can access ride")
} else if customerHeight >= 120 {
println("customer can access children's rides")
} else {
println("customer is too small")
}
}
Output
$ go run main.go
If Statements in Go
can access ride