golang规范中方法值部分的'non-interface method'是什么意思?
What is meant by 'non-interface method' in the section about method values in the golang specification?
The Go Programming Language Specification says:
As with selectors, a reference to a non-interface method with a value receiver using a pointer will automatically dereference that pointer: pt.Mv is equivalent to (*pt).Mv.
和:
As with method calls, a reference to a non-interface method with a pointer receiver using an addressable value will automatically take the address of that value: t.Mp is equivalent to (&t).Mp.
那么,给定上下文中的非接口方法是什么?
接口方法是指您引用(您调用)的方法是对接口值(其方法集包含该方法)的调用。同样,非接口方法意味着您引用(调用)的方法不是对接口值的调用(而是对具体类型的调用)。
例如:
var r io.Reader = os.Stdin
r.Read(nil) // Interface method: type of r is an interface (io.Reader)
var p image.Point = image.Point{}
p.String() // Non-interface method, p is a concrete type (image.Point)
为了演示自动取消引用和地址获取,请参见此示例:
type myint int
func (m myint) ValueInt() int { return int(m) }
func (m *myint) PtrInt() int { return int(*m) }
func main() {
var m myint = myint(1)
fmt.Println(m.ValueInt()) // Normal
fmt.Println(m.PtrInt()) // (&m).PtrInt()
var p *myint = new(myint)
*p = myint(2)
fmt.Println(p.ValueInt()) // (*p).ValueInt()
fmt.Println(p.PtrInt()) // Normal
}
它输出(在 Go Playground 上尝试):
1
1
2
2
type T struct {}
func (t *T) f() {}
func main() {
x := T{}
x.f()
}
以上,x.f为非接口方式
The Go Programming Language Specification says:
As with selectors, a reference to a non-interface method with a value receiver using a pointer will automatically dereference that pointer: pt.Mv is equivalent to (*pt).Mv.
和:
As with method calls, a reference to a non-interface method with a pointer receiver using an addressable value will automatically take the address of that value: t.Mp is equivalent to (&t).Mp.
那么,给定上下文中的非接口方法是什么?
接口方法是指您引用(您调用)的方法是对接口值(其方法集包含该方法)的调用。同样,非接口方法意味着您引用(调用)的方法不是对接口值的调用(而是对具体类型的调用)。
例如:
var r io.Reader = os.Stdin
r.Read(nil) // Interface method: type of r is an interface (io.Reader)
var p image.Point = image.Point{}
p.String() // Non-interface method, p is a concrete type (image.Point)
为了演示自动取消引用和地址获取,请参见此示例:
type myint int
func (m myint) ValueInt() int { return int(m) }
func (m *myint) PtrInt() int { return int(*m) }
func main() {
var m myint = myint(1)
fmt.Println(m.ValueInt()) // Normal
fmt.Println(m.PtrInt()) // (&m).PtrInt()
var p *myint = new(myint)
*p = myint(2)
fmt.Println(p.ValueInt()) // (*p).ValueInt()
fmt.Println(p.PtrInt()) // Normal
}
它输出(在 Go Playground 上尝试):
1
1
2
2
type T struct {}
func (t *T) f() {}
func main() {
x := T{}
x.f()
}
以上,x.f为非接口方式