I skimmed through math package in Golang GitHub repo. Some of them have two difference function declarations. In the code below, sqrt.go
has Sqrt
and sqrt
func. My questions are: Why do they implement it this way? What are the benefits? Is this because of the exported and unexported identifier (lowercase vs uppercase first letter)?
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()
According to the spec, function declarations without a body, as you show in the first line of the code you quoted, provide a declaration for code implemented outside of Go; usually in assembly or another language.
In this case, most implementations for that func Sqrt(x float64) float64
are in assembly; for example 386, amd64, arm, etc.
Sample from amd64:
// func Sqrt(x float64) float64
TEXT ·Sqrt(SB),NOSPLIT,$0
SQRTSD x+0(FP), X0
MOVSD X0, ret+8(FP)
RET
For architectures that have no assembly implementation the assembly simply refers to the unexported / private func sqrt(x float64) float64
version that is in Go. For example the mips64 architecture.
TEXT ·Sqrt(SB),NOSPLIT,$0
JMP ·sqrt(SB)