为什么向函数处理程序分配方法有效。
短链接:https://play.golang.org/p/UEYGCpMgyV6
package main
import (
"fmt"
"math"
)
type Vertex struct {
X, Y float64
}
func (v *Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
func Abs() float64 {
return math.Sqrt(10)
}
func AbsFloat(f float64) float64 {
return math.Sqrt(f)
}
func main() {
v := Vertex{3, 4}
// Assigning Abs() to func handler.
var absFunc func() float64 = Abs
fmt.Println(absFunc())
// Wrong type as expected.
//absFunc = AbsFloat
// Assigning method to func handler works, why ?
absFunc = v.Abs
// Changing receiver args. Useful for unittesting but why/how this works ?
v.X = 1
v.Y = 1
fmt.Println(absFunc())
}
是Golang类型检查func()float64 == func(T)float64或其他东西的“功能”吗?请帮助理解这个概念。
答案 0 :(得分:1)