Golang. Lessons for beginners


Go Conditions

Conditional statements are used to perform different actions based on different conditions.

Go Conditions

A condition can be either true or false.

Go supports the usual comparison operators from mathematics:

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

Additionally, Go supports the usual logical operators:

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

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