英文:
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 < 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论