A Tour of Go If

The if statement looks as it does in C or Java, except that the ( ) are gone and the { } are required.

(Sound familiar?)

 

复制代码
package main  

import (
    "fmt"
    "math"
)

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

func main() {
    fmt.Println(sqrt(2), sqrt(-4))
}
复制代码

 

posted @   wuhn  阅读(164)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示