Introduction
Programs often need to choose what to do next. Conditional statements let your code take a different action depending on how a condition turns out.
Go Conditions
Every condition comes down to one of two answers: true or false.
To build one, you can use the comparison operators you know from math:
- Equal to
== - Not equal to
!= - Less than
< - Less than or equal
<= - Greater than
> - Greater than or equal
>=
The familiar logical operators are available as well:
- Logical AND
&& - Logical OR
|| - Logical NOT
!
Use them alone or mix them to express whatever decision your program has to make.
Go Conditional Statements
Go offers four conditional statements to act on those answers:
ifruns a block of code when its condition is trueelseruns a block of code when that same condition is falseelse ifadds another condition to check when the first one turned out falseswitchchooses among many possible blocks of code