Go Conditions

Go Conditions #

Conditional statements are used to perform different actions depending on whether a condition is true or false.

A condition in Go can evaluate to either true or false.


Comparison Operators #

Go supports the usual mathematical comparison operators:

OperatorMeaning
<Less than
<=Less than or equal to
>Greater than
>=Greater than or equal to
==Equal to
!=Not equal to

Logical Operators #

Go also supports logical operators to combine or reverse conditions:

OperatorMeaning
&&Logical AND
||Logical OR
!Logical NOT

You can combine these operators to create complex conditions:

x > y
x != y
(x > y) && (y > z)
(x == y) || z

Conditional Statements in Go #

Go provides several statements to control program flow:

  • if: Execute a block of code if a specified condition is true.
  • else: Execute a block of code if the same condition is false.
  • else if: Test a new condition if the first condition is false.
  • switch: Specify many alternative blocks of code to execute based on different values.