Fundamentals 7 min read

Master Go's Conditional Statements: if, else, and Nested Logic Explained

This guide explains Go's conditional statements, covering the basic if syntax, the use of else and else‑if clauses, initialization statements, limitations such as the lack of a ternary operator, and provides multiple practical code examples illustrating simple, chained, and nested conditions.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Go's Conditional Statements: if, else, and Nested Logic Explained

1.1.1. Go Conditional Statements

Conditional statements let developers specify one or more conditions, test whether they are true, and execute designated statements when true, otherwise execute alternative statements.

Go provides several forms of conditional statements.

1.1.2. if Statement – an if statement consists of a Boolean expression followed by one or more statements.

The syntax of the if statement in Go is as follows:

• Parentheses around the condition expression can be omitted.<br/>• An initialization statement is supported, allowing definition of block‑local variables.<br/>• The opening brace must appear after the condition expression.<br/><br/>if booleanExpression {<br/>    /* executed when the Boolean expression is true */<br/>}<br/>

If the Boolean expression evaluates to true, the following block is executed; otherwise it is skipped.

x := 0<br/><br/>// if x > 10        // Error: missing condition in if statement<br/>// {<br/>// }<br/><br/>if n := "abc"; x > 0 {<br/>    // Initialization statement can be anything, e.g., println("init")<br/>    println(n[2])<br/>} else if x < 0 {<br/>    // Note the position of the opening brace for else if and else<br/>    println(n[1])<br/>} else {<br/>    println(n[0])<br/>}<br/>
* Go does not support the ternary operator (a > b ? a : b).

Example:

package main<br/><br/>import "fmt"<br/><br/>func main() {<br/>   // Define a local variable<br/>   var a int = 10<br/>   // Use an if statement to evaluate a Boolean expression<br/>   if a < 20 {<br/>       // Executed when the condition is true<br/>       fmt.Printf("a is less than 20
")<br/>   }<br/>   fmt.Printf("The value of a is : %d
", a)<br/>}<br/>

The output of the above code is:

a is less than 20<br/>The value of a is : 10<br/>

1.1.3. if...else Statement – an optional else clause executes when the Boolean expression is false.

Syntax

In Go, the syntax of the if...else statement is:

if booleanExpression {<br/>   /* executed when true */<br/>} else {<br/>   /* executed when false */<br/>}<br/>

If the Boolean expression is true, the if block runs; otherwise the else block runs.

Example:

package main<br/><br/>import "fmt"<br/><br/>func main() {<br/>   // Local variable definition<br/>   var a int = 100<br/>   // Evaluate Boolean expression<br/>   if a < 20 {<br/>       fmt.Printf("a is less than 20
")<br/>   } else {<br/>       fmt.Printf("a is not less than 20
")<br/>   }<br/>   fmt.Printf("The value of a is : %d
", a)<br/>}<br/>

The output of the above code is:

a is not less than 20<br/>The value of a is : 100<br/>

1.1.4. Nested if Statements – you can embed one or more if or else if statements inside an if or else if.

Syntax

The syntax for nested if statements in Go is:

if booleanExpression1 {<br/>   /* executed when expression1 is true */<br/>   if booleanExpression2 {<br/>       /* executed when expression2 is true */<br/>   }<br/>}<br/>

You can similarly nest else if…else statements inside an if block.

Example

package main<br/><br/>import "fmt"<br/><br/>func main() {<br/>   var a int = 100<br/>   var b int = 200<br/>   if a == 100 {<br/>       if b == 200 {<br/>           fmt.Printf("a is 100, b is 200
")<br/>       }<br/>   }<br/>   fmt.Printf("a value : %d
", a)<br/>   fmt.Printf("b value : %d
", b)<br/>}<br/>

The output of the above code is:

a is 100, b is 200<br/>a value : 100<br/>b value : 200<br/>

Source: https://www.topgoer.com/流程控制/条件语句if.html

(© Original author, all rights reserved)

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Goprogramming fundamentalsif-elseconditional statements
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.