Skip to content

if/else

https://gobyexample-cn.github.io/if-else

在 Go 中,花括号是必需的。

go
	
    if 7%2 == 0 {
        fmt.Println("7 is even")
    } else {
        fmt.Println("7 is odd")
    }
go
    if num := 9; num < 0 {
        fmt.Println(num, "is negative")
    } else if num < 10 {
        fmt.Println(num, "has 1 digit")
    } else {
        fmt.Println(num, "has multiple digits")
    }
if <simple statement> ; <condition> {
    ...
}

只能是一条语句(statement)

go
if a, b := func() (int, int) {
    return 1, 2
}(); a < b {
    fmt.Println(a, b)
}