Tutorials References Exercises Menu
Paid Courses

Go Conditions


Go Conditions

Conditions help in decision making. A condition is either true or false.

Go supports the usual comparison operators from mathematics:

  • Less than x < y
  • Less than or equal x <= y
  • Greater than x > y
  • Greater than or equal x >= y
  • Equal to x == y
  • Not equal to x != y

Additionally, Go supports the usual logical operators:

  • Logical AND x && y
  • Logical OR x || y
  • Logical NOT !x

You can use these operators or their combinations to create conditions for different decisions.

Example Try it
x > y Try it »
x != y Try it »
(x > y) && (y > z) Try it »
(x == y) || z Try it »

Go has the following conditional statements:

  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative blocks of code to be executed
  • Use select is similar to switch but is used for channel communication.