在Go中,为什么没有直接计算整数数据类型绝对值的函数?目前,所有整数值都必须转换为float64,然后传递给math.Abs()函数,该函数返回一个float64数字,然后再将其转换回整数。这段代码会引起错误“./prog.go:12:39: cannot use x (type int64) as type float64 in argument to math.Abs”,因为Go是一种静态类型语言,所以它不允许使用不同的数据类型。
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println("Hello, playground")
var x int64 = -10
fmt.Println("Abolute value ", math.Abs(x))
}