没有函数体的函数签名 [英] Function signature with no function body

查看:23
本文介绍了没有函数体的函数签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看 math 的源代码时.Ceil 方法,我发现了这个语法,其中有一个没有主体的导出函数签名,以及一个包含实现的相同签名的非导出版本:

//Ceil 返回大于或等于 x 的最小整数值.////特殊情况是://天花板(±0) = ±0//Ceil(±Inf) = ±Inf//Ceil(NaN) = NaNfunc Ceil(x float64) float64功能细胞(x float64)float64 {返回 -Floor(-x)}

我认为这是一些允许您轻松导出本地函数的语法.那是对的吗?为什么要这样做而不是仅仅拥有一个导出的函数并在包中使用它?

解决方案

根据 Go 语言规范.><块引用>

函数声明可以省略主体.这样的声明为在 Go 之外实现的函数提供了签名,例如汇编例程.

在这种情况下,Ceil 函数是由 floor_386.s 中的 386 的架构特定程序集文件实现的.amd64 和 arm 架构都有一个实现 Ceil() 的汇编文件,但这些汇编文件只是调用未导出的 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?

解决方案

According to the Go language specification.

A function declaration may omit the body. Such a declaration provides the signature for a function implemented outside Go, such as an assembly routine.

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.

这篇关于没有函数体的函数签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆