Backend Development 9 min read

Flow Control in Go (Golang): if/else, for, switch, break, continue, and goto

This article explains Go's flow‑control constructs—including conditional statements, loops, switch branches, and special statements like break, continue, and goto—provides syntax details, highlights key characteristics, and offers multiple code examples to help developers write clear and efficient Go code.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Flow Control in Go (Golang): if/else, for, switch, break, continue, and goto

Flow control is one of the core concepts of programming languages, determining the execution order and logical branches of code. In Go (Golang), flow control mainly includes conditional statements ( if/else ), loop statements ( for ), branch statements ( switch ) and special control statements such as break , continue , and goto .

This article details Go's flow‑control mechanisms and provides code examples to help you master writing efficient and clear Go code.

1. Conditional Statements (if/else)

Go's if statement is used for conditional judgment. Its syntax is similar to C/Java but more concise: no parentheses are required, and an initialization statement can be placed before the condition.

Basic Syntax

if condition {
    // executed when condition is true
} else if condition2 {
    // executed when condition2 is true
} else {
    // executed otherwise
}

Example

package main

import "fmt"

func main() {
    x := 10
    // basic if-else
    if x > 5 {
        fmt.Println("x is greater than 5")
    } else {
        fmt.Println("x is not greater than 5")
    }
    // if with initialization statement
    if y := 20; y > x {
        fmt.Println("y is greater than x")
    }
}

Features

if can contain an initialization statement (e.g., if x := 10; x > 5 ); the variable's scope is limited to the if block.

else if and else are optional and can be nested.

2. Loop Statements (for)

Go only has the for loop; there is no while or do‑while , but for can simulate all loop structures.

Basic Syntax

for initialization; condition; post {
    // loop body
}

Example

package main

import "fmt"

func main() {
    // standard for loop
    for i := 0; i < 5; i++ {
        fmt.Println(i) // 0, 1, 2, 3, 4
    }
    // while‑like form
    j := 0
    for j < 5 {
        fmt.Println(j) // 0, 1, 2, 3, 4
        j++
    }
    // infinite loop (usually combined with break)
    k := 0
    for {
        if k >= 5 {
            break
        }
        fmt.Println(k) // 0, 1, 2, 3, 4
        k++
    }
}

Features

for can replace while by omitting the initialization and post statements.

An infinite loop is written as for {} and typically uses break to exit.

The range keyword can be used with for to iterate over arrays, slices, maps, etc.

3. Branch Statements (switch)

Go's switch is more flexible than C/Java; it can match values, expressions, and even types (type switch).

Basic Syntax

switch expression {
case value1:
    // executed when value1 matches
case value2, value3:
    // executed when value2 or value3 matches
default:
    // default case
}

Example

package main

import "fmt"

func main() {
    day := "Monday"
    // basic switch
    switch day {
    case "Monday":
        fmt.Println("星期一")
    case "Tuesday":
        fmt.Println("星期二")
    default:
        fmt.Println("其他")
    }
    // switch with expression (if‑else chain)
    score := 85
    switch {
    case score >= 90:
        fmt.Println("优秀")
    case score >= 80:
        fmt.Println("良好")
    default:
        fmt.Println("一般")
    }
    // multiple value matching
    num := 2
    switch num {
    case 1, 3, 5:
        fmt.Println("奇数")
    case 2, 4, 6:
        fmt.Println("偶数")
    }
}

Features

switch does not require an explicit break ; Go automatically exits after the first matching case unless fallthrough is used.

A switch can be written without an expression, acting like an if‑else chain.

A case can match multiple values (e.g., case 1, 2, 3 ).

4. Other Control Statements

(1) break and continue

break : immediately exits the current loop.

continue : skips the remainder of the current iteration and proceeds to the next one.

for i := 0; i < 10; i++ {
    if i == 5 {
        break // exit loop
    }
    if i%2 == 0 {
        continue // skip even numbers
    }
    fmt.Println(i) // 1, 3, 5, 7, 9
}

(2) goto (use with caution)

goto can jump to a labeled statement but often makes code hard to follow and is not recommended.

func main() {
    i := 0
Loop:
    if i < 5 {
        fmt.Println(i)
        i++
        goto Loop // jump back to Loop label
    }
}

5. Summary

Statement

Purpose

Example

if/else

Conditional judgment

if x > 5 { ... }
for

Looping

for i := 0; i < 5; i++
switch

Multiple‑branch selection

switch day { case "Mon": ... }
break

Exit loop

break
continue

Skip current iteration

continue
goto

Jump (use sparingly)

goto Label

Go's flow‑control design is concise yet powerful; mastering these structures enables you to write more efficient code.

Java learning material

C language learning material

Frontend learning material

C++ learning material

PHP learning material

golangProgrammingGoflow controlif-elseswitchfor loop
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login 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.