Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Go Language Conditional Statements

Go Language Conditional Statements

Conditional statements require the developer to specify one or more conditions and decide whether to execute specified statements by testing whether the condition is true, and to execute other statements when the condition is false.

The following figure shows the structure of conditional statements in programming languages:

Go provides the following conditional judgment statements:

Statement Description
if statement if statement Consists of a Boolean expression followed by one or more statements.
if...else statement if statement An optional ... can be used after else statement, and the expression in the else statement is executed when the Boolean expression is false.
nested if statements You can if or else if Embed one or more within the statement if or else if if statement.
switch statement switch This statement is used to perform different actions based on different conditions.
The select statement select The statement is similar to the switch statement, but select randomly executes one runnable case. If no case is runnable, it blocks until a case becomes runnable.

Note: Go has no ternary operator, so it does not support ?: form of conditional expression.