Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 将成员函数作为参数传递时发生编译错误_Go - Fatal编程技术网

Go 将成员函数作为参数传递时发生编译错误

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

我正在为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
}

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{
    归零
    }