freeCodeCamp/guide/chinese/go/if/index.md

1.1 KiB

title localeTitle
If in Go 如果在Go

如果在Go

Go的if语句就像for循环一样;表达式不必被括号括起来 ( )但是大括号{ }是必需的。

func sqrt(x float64) string { 
    if x < 0 { 
        return sqrt(-x) + "i" 
    } 
    return fmt.Sprint(math.Sqrt(x)) 
 } 

for中, if语句可以用很短的语句中的条件之前执行启动。

声明声明的变量仅在范围内,直到if结束。

func pow(x, n, lim float64) float64 { 
    if v := math.Pow(x, n); v < lim { 
        return v 
    } 
    return lim 
 } 

如果是,否则

if short语句中声明的变量也可以在任何else块中使用。

package main 
 
 import ( 
    "fmt" 
    "math" 
 ) 
 
 func pow(x, n, lim float64) float64 { 
    if v := math.Pow(x, n); v < lim { 
        return v 
    } else { 
        fmt.Printf("%g >= %g\n", v, lim) 
    } 
    // can't use v here, though 
    return lim 
 } 
 
 func main() { 
    fmt.Println( 
        pow(3, 2, 10), 
        pow(3, 3, 20), 
    ) 
 } 

运行上述程序会产生类似于以下输出的输出 - $ go run if.go 27 >= 20 9 20