If... (Asking Questions)
When you want the computer to do different things depending on a situation, use if.
Ask a question
In Go, questions are clean and simple.
number := 15
if number > 10 {
fmt.Println("It is greater than 10!")
}
Translation:
Remember that number is 15.
If number is greater than 10 {
print "It is greater than 10!"
}
The difference from Python is that Go uses braces { } to define the block.
No! (else)
If the answer is no, use else.
if number > 10 {
fmt.Println("It is big!")
} else {
fmt.Println("It is small or equal!")
}
Ask more than once (else if)
If you want to ask another question, use else if.
if number > 20 {
fmt.Println("It is greater than 20!")
} else if number > 10 {
fmt.Println("It is greater than 10 and 20 or less!")
} else {
fmt.Println("It is 10 or less!")
}
Translation:
If number is greater than 20 { ... }
Otherwise, if number is greater than 10 { ... }
Otherwise { ... }
Indentation and braces
In Python, indentation was everything. In Go, braces { } mark the start and end of the block.
GoLand still formats with indentation so your code is easy to read.
The closing brace means, “the question ends here.”
Symbols recap
==: equal?!=: not equal?>: greater than?<: less than?
Now you can give Go choices based on conditions.