I am going through the Golang tutorials on their website and am confused by code similar to this that I've simplified and reproduced here:
package main
import (
"fmt"
"math"
)
func main() {
a := math.Sqrt2
fmt.Println(a)
}
This prints 1.4142135623730951
in the sandbox. Replacing a := math.Sqrt2
with a := math.Sqrt(2)
does the same thing but I'm confused how the function can be called without parentheses. math.Sqrt
is not a function pointer here (there is no math.Sqrt2
function anyway, it's a function being passed without any parentheses. The function in the Go documentation here is listed as: func Sqrt(x float64) float64
i.e. with the parameter. So how does that work? Is it just because math.Sqrt()
is a simplistic function that Go can assume it's a float64
without the parentheses passed? Am I missing something?
If it helps, I found this phenomenon here in the tutorials on line 14, originally. If anyone could explain this feature to me, that would be awesome. I'd love to learn about it.
There is nothing special happening here. math.Sqrt2
is a constant. You can find the other constants in the math
package in the docs.
In general, go doesn't really have any "magic". So if something feels a bit magical, its more than likely just a misunderstanding.