package main
func TopLevel(x int) {}
type Pointer struct{}
func (*Pointer) M(int) {}
type Value struct{}
func (Value) M(int) {}
type Interface interface { M(int) }
var literal = func(x int) {}
func main() {
// direct call of top-level func
TopLevel(1)
// direct call of method with value receiver (two spellings, but same)
var v Value
v.M(1)
Value.M(v, 1)
// direct call of method with pointer receiver (two spellings, but same)
var p Pointer
(&p).M(1)
(*Pointer).M(&p, 1)
// indirect call of func value (×4)
f1 := TopLevel
f1(1)
f2 := Value.M
f2(v, 1)
f3 := (*Pointer).M
f3(&p, 1)
f4 := literal
f4(1)
// indirect call of method on interface (×3)
var i Interface
i = v
i.M(1)
i = &v
i.M(1)
i = &p
i.M(1)
Interface.M(i, 1)
Interface.M(v, 1)
Interface.M(&p, 1)
}
综上,总共有10种函数与调用方式的组合:
direct call of top-level func /
direct call of method with value receiver /
direct call of method with pointer receiver /
indirect call of func value / set to top-level func
indirect call of func value / set to value method
indirect call of func value / set to pointer method
indirect call of func value / set to func literal
indirect call of method on interface / containing value with value method
indirect call of method on interface / containing pointer with value method
indirect call of method on interface / containing pointer with pointer method
Go里一共有4种函数:
调用的方式有5种:
综上,总共有10种函数与调用方式的组合:
//refer: https://docs.google.com/document/d/1bMwCey-gmqZVTpRax-ESeVuZGmjwbocYs1iHplK-cjo/pub