gpt4 book ai didi

go - Go解释中导出和未导出

转载 作者:IT王子 更新时间:2023-10-29 01:02:11 25 4
gpt4 key购买 nike

我浏览了 Golang GitHub 存储库中的数学包。其中一些有两个不同的函数声明。在下面的代码中,sqrt.goSqrtsqrt 函数。我的问题是:他们为什么要这样实现?有什么好处?这是因为导出和未导出的标识符(小写与大写首字母)?

func Sqrt(x float64) float64

// Note: Sqrt is implemented in assembly on some systems.
// Others have assembly stubs that jump to func sqrt below.
// On systems where Sqrt is a single instruction, the compiler
// may turn a direct call into a direct use of that instruction instead.

func sqrt(x float64) float64 {

math.Sqrt()

最佳答案

根据spec ,没有主体的函数声明,如您在 first line of the code you quoted 中所示, 为在 Go 之外实现的代码提供声明;通常使用汇编或其他语言。

在这种情况下,func Sqrt(x float64) float64 的大多数实现都在汇编中;例如 386 , amd64 , arm

来自 amd64 的示例:

// func Sqrt(x float64) float64
TEXT ·Sqrt(SB),NOSPLIT,$0
SQRTSD x+0(FP), X0
MOVSD X0, ret+8(FP)
RET

对于没有程序集实现的架构,程序集只是指 Go 中未导出/私有(private)的 func sqrt(x float64) float64 版本。例如 mips64架构。

TEXT ·Sqrt(SB),NOSPLIT,$0
JMP ·sqrt(SB)

关于go - Go解释中导出和未导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36430469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com