golang / tour

[mirror] A Tour of Go
BSD 3-Clause "New" or "Revised" License
1.55k stars 519 forks source link

tour: [Could not Understand the context] #938

Open Mukundhan-I2I opened 4 years ago

Mukundhan-I2I commented 4 years ago

Context: https://tour.golang.org/flowcontrol/10

Change the title above to describe your issue and add your feedback here, including code if necessary


switch i {
case 0:
case f():
}
romanegunkov commented 2 years ago

From https://go.dev/tour/flowcontrol/9

Another important difference is that Go's switch cases need not be constants

For example:

package main

import "fmt"

func main() {
    switch i := 2; i {
    case 0:
        fmt.Println("i == 0")
    case f1():
        fmt.Println("i == resault of func f1()")
    case f2():
        fmt.Println("i == resault of func f2()")
    case f3():
        fmt.Println("i == resault of func f3()")
    }
}

func f1() int {
    fmt.Println("Inside func f1()")
    return 1
}

func f2() int {
    fmt.Println("Inside func f2()")
    return 2
}

func f3() int {
    fmt.Println("Inside func f3()")
    return 3
}
Inside func f1()
Inside func f2()
i == resault of func f2()

Program exited.

I think this example in the context may be write more clear.