为什么Go包的代码没有主体?

huangapple go评论109阅读模式
英文:

Why go package code has no body?

问题

我在学习导出打包时,对Go包的编码风格产生了疑问。

我发现源代码中的包(例如:"math","fmt")的函数以大写字母开头,但没有函数体。

我按照这种代码编写,但不起作用。

有人可以解释为什么吗?

func Abs(x float64) float64

func abs(x float64) float64 {
    switch {
    case x < 0:
        return -x
    case x == 0:
        return 0 // 返回正确的 abs(-0)
    }
    return x
}
英文:

I wondered coding style of go package when studying exporting packaging.

and found that the code of package in source(ex:"math", "fmt") has

function begins with a capital letter but no body.

I just follow this code but doesn't work.

Anyone who explain why??

func Abs(x float64) float64

func abs(x float64) float64 {
	switch {
	case x &lt; 0:
		return -x
	case x == 0:
		return 0 // return correctly abs(-0)
	}
	return x
}

答案1

得分: 6

这些函数是用汇编语言实现的(https://golang.org/doc/asm),它们有特定于平台的实现。你可以查看例如$GOROOT/src/math/abs_amd64.s文件来了解func Abs(x float64) float64函数的实现。

英文:

those functions are implemented in Assembler (https://golang.org/doc/asm)
there are platform specific implementations for them: take a look at e.g.:

$GOROOT/src/math/abs_amd64.s

for

func Abs(x float64) float64

huangapple
  • 本文由 发表于 2015年8月10日 00:46:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/31906504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定