没有功能体的功能签名

编程入门 行业动态 更新时间:2024-10-27 00:32:08
本文介绍了没有功能体的功能签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当查看 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.

更多推荐

没有功能体的功能签名

本文发布于:2023-10-19 14:30:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1507805.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:功能

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!