当查看 math.Ceil 方法,我找到了这种语法,其中有一个不带主体的导出函数签名,以及包含实现的相同签名的非导出版本:
When viewing the source for the math.Ceil method, I found this syntax where there's an exported function signature with no body, and a non-exported version of the same signature that includes the implementation:
// Ceil returns the least integer value greater than or equal to x. // // Special cases are: // Ceil(±0) = ±0 // Ceil(±Inf) = ±Inf // Ceil(NaN) = NaN func Ceil(x float64) float64 func ceil(x float64) float64 { return -Floor(-x) }我假设这是一些允许您轻松导出本地函数的语法。那是对的吗?为什么会这样做,而不是只有一个导出的函数,并在包中使用它?
I assume this is some syntax which allows you to easily export a local function. Is that correct? And why would one do this instead of just having a single exported function and using it within the package?
推荐答案根据< Go language specification 。
A函数声明可以省略body。这样的声明提供了在Go之外实现的函数的签名,例如汇编例程。
A function declaration may omit the body. Such a declaration provides the signature for a function implemented outside Go, such as an assembly routine.
在这种情况下, Ceil 函数是由体系结构特定的程序集文件实现的,其格式为 floor_386.s 中的386。 amd64和arm架构每个都有一个实现 Ceil()的程序集文件,但是这些程序集文件只是粘贴来调用未导出的 ceil ()函数。
In this case, the Ceil function is implemented by an architecture specific assembly file for 386 in floor_386.s. Both the amd64 and arm architectures each have an assembly file that implements Ceil() as well, but those assembly files are just glue to call the unexported ceil() function.
更多推荐
没有功能体的功能签名
发布评论