Conditionals allow your program to make decisions and execute different code based on whether certain conditions are true or false. They're essential for controlling the flow of your program.
The most basic conditional in Go is the if statement. It executes a block of code only when a condition is true.
age := 18
if age >= 18 {
fmt.Println("You are an adult")
}
Notice that Go doesn't require parentheses around the condition, but the curly braces {} are required even for single-line blocks.
Go provides several operators for comparing values:
// Equal to
if x == 5 { }
// Not equal to
if x != 5 { }
// Greater than
if x > 5 { }
// Less than
if x < 5 { }
// Greater than or equal to
Conditionals are commonly used inside functions to control behavior based on parameters.
func checkAge(age int) {
if age >= 18 {
fmt.Println("Adult")
}
}
checkAge(20) // Prints: Adult
checkAge(15) // Prints: Minor
For this challenge, you'll implement the inside of a function called checkPowerLevel that accepts a parameter called powerLevel (an unsigned integer).
Your task is to check if the powerLevel is greater than 9000. If it is, print the message "It's over 9000!!!".
The function signature is already provided:
func checkPowerLevel(powerLevel uint) {
// Your code here
}