Go 語言函數(shù)作為值
Go 語言可以很靈活的創(chuàng)建函數(shù),并作為值使用。以下實(shí)例中我們在定義的函數(shù)中初始化一個變量,該函數(shù)僅僅是為了使用內(nèi)置函數(shù) math.sqrt() ,實(shí)例為:
package main
import (
"fmt"
"math"
)
func main(){
/* 聲明函數(shù)變量 */
getSquareRoot := func(x float64) float64 {
return math.Sqrt(x)
}
/* 使用函數(shù) */
fmt.Println(getSquareRoot(9))
}
以上代碼執(zhí)行結(jié)果為:
3
更多建議: