← Back to articles

Learning Go (6): conditions and loops

Part 6 of my Go fundamentals notes: syntax, examples, and lessons learned.

Conditions and for loops

1. if conditions

1.1. Basic if syntax

Go’s if resembles other languages. Parentheses around the condition are normally omitted, though they can clarify parts of a complex expression.

age := 10
//1、加括号
if age > 18 {
    fmt.Println("你已经成年了")
}
//2、不加括号
if (age > 18){
    fmt.Println("你已经成年了")
}

1.2. if-else

age := 10
if age > 18 {
    fmt.Println("你已经成年了")
} else {
    fmt.Println("你还没有成年")
}

// else if
if age > 20 {
    fmt.Println("你已经成年了")
} else if a18 > 16 {
    fmt.Println("你已经成年了")
} else {
    fmt.Println("你还没有成年")
}

2. for loops

Go has no separate while statement; use for.

2.1. Basic for syntax

//基础写法
for i := 0; i < 10; i++ {
    fmt.Println(i)
}
//注意不能加括号

//for循环代替while循环
var i int = 0
for {
    if i > 10 {
        break
    }
    fmt.Println(i)
    i++
}

2.2. Examples

2.2.1. Print all even numbers from 1 to 100

for i := 1; i <= 1--; i++{
    if i % 2 == 0{
        fmt.Println(i)
    }
}

2.2.2. Print a multiplication table

for i := 1; i <= 9; i++{
    for j := 1; j <= i; j++{
        fmt.Printf("%d * %d = %d\t", j, i, i * j)
    }
    fmt.Println()
}

2.3. Using range

range iterates over arrays, slices, strings, maps, and channels. The values it yields depend on the operand: for example, an index and element for slices, a key and value for maps, or a received value for channels.

name := "hello go, hello world"

for index, value := range name {
    fmt.Printf("index = %d, value = %c\n", index, value)
}

for _, value := range name {
    fmt.Printf("value = %c\n", value)
}

for index := range name {
    fmt.Printf("index = %d\n", index)
    fmt.Printf("value = %c\n", name[index])
}

Note: go

2.4. Controlling a loop

Use break and continue to control loop execution.

continue skips the remaining statements in the current iteration and moves to the next.

break exits the loop and continues after it.

3. goto

goto jumps to a labeled statement. Use it sparingly: arbitrary jumps can make code harder to read and maintain.

A labeled jump can sometimes centralize cleanup or error handling, though ordinary control structures are usually clearer.

for i := 0; i < 10; i++ {
    for j := 0; j < 10; j++ {
        if j == 5 {
            goto over
        }
        fmt.Println(i, j)
    }
}

//任意代码块
over:
    fmt.Println("over")
//代码直接跳转到over代码块中运行

4. switch

switch executes different branches according to conditions and can replace a chain of if-else statements.

It can make the cases easier to read.

//基础写法
switch var1{
    case val1:
       ...
    case val2:
       ...
    default:
       ...
}

//举例--中文的星期几,输出对应的英文
day := "星期一"

switch dat{
    case "星期一":
        fmt.Println("Monday")
    case "星期二":
        fmt.Println("Tuesday")
    case "星期三":
        fmt.Println("Wednesday")
    case "星期四":
        fmt.Println("Thursday")
    case "星期五":
        fmt.Println("Friday")
    case "星期六":
        fmt.Println("Saturday")
    case "星期日":
        fmt.Println("Sunday")
    default:
        fmt.Println("Unknown")
}

siwtch {
    case day == "星期一":
        fmt.Println("Monday")
    case day == "星期二":
        fmt.Println("Tuesday")
    case day == "星期三":
        fmt.Println("Wednesday")
    case day == "星期四":
        fmt.Println("Thursday")
    case day == "星期五":
        fmt.Println("Friday")
    case day == "星期六":
        fmt.Println("Saturday")
    case day == "星期日":
        fmt.Println("Sunday")
    default:
        fmt.Println("Unknown")
}

score := 90

switch {
    case score >= 90:
        fmt.Println("A")
    case score >= 80:
        fmt.Println("B")
    case score >= 70:
        fmt.Println("C")
    case score >= 60:
        fmt.Println("D")
    default:
        fmt.Println("E")
}

Go cases do not fall through automatically. No break is needed to finish a case; use fallthrough explicitly when that behavior is intended.

About this article’s date

The date recorded on the old site is retained; writing may have started earlier.

In this series · Go fundamentals

PreviousLearning Go (5): strings ←NextLearning Go (7): arrays, slices, maps, and lists →