Go 将成员函数作为参数传递时发生编译错误
我正在为Rob Pike在本次演讲中概述的状态机实现建模 但我无法编译它。我提供了一个失败的小样本 调用:m:=New(foo) 失败于Go 将成员函数作为参数传递时发生编译错误,go,Go,我正在为Rob Pike在本次演讲中概述的状态机实现建模 但我无法编译它。我提供了一个失败的小样本 调用:m:=New(foo) 失败于 ./main.go:31:11: undefined: foo 我试过了 m := New(M.foo) m := New(foo(*M)) 我不知道这个的正确语法 package main type StateFunc func(*M) StateFunc type M struct { start StateFunc
./main.go:31:11: undefined: foo
我试过了
m := New(M.foo)
m := New(foo(*M))
我不知道这个的正确语法
package main
type StateFunc func(*M) StateFunc
type M struct {
start StateFunc
}
func New(start StateFunc) *M {
return &M{
start: start,
}
}
func (m *M) foo() StateFunc {
return nil
}
func (m *M) Start() {
go m.run()
}
func (m *M) run() {
state := m.start
for state != nil {
state = state(m)
}
}
func main() {
m := New(foo)
}
我希望它可以编译,但我不知道正确的语法使它工作
(m*m)foo()
与类型StateFunc func(*m)StateFunc
foo
是一种方法,它有一个接收器*M
,没有接收器就不能使用它foo
:
func foo(*M)StateFunc{
归零
}
(m*m)foo()
与类型StateFunc func(*m)StateFunc
foo
是一种方法,它有一个接收器*M
,没有接收器就不能使用它foo
:
func foo(*M)StateFunc{
归零
}