对不起,我再次发布我的问题。如何将结构的方法作为参数传递给使用golang的另一个函数
我在问我的问题之前已经阅读了解决方案。我认为它不能帮助我,因为我的问题是如何传递一个函数作为参数?我不想打电话给它。
我只是想将它传递给我不能修改另一个函数(或者我不希望编辑),我想用一个字符串变量指向功能
funcName := "Go"
m.set(t.funcName)
我认为这是从这个问题Call a Struct and its Method by name in Go?
例如不同
我有这样一个功能:
type Context struct{}
type myclass struct{}
type Handler func (c *Context)
func (r *myclass) set(ch Handler) {
}
我可以用这样的方式:
type testclass struct {}
func (t *testclass) Go(c *Context){
println("Hello");
}
t := &testclass{}
m := &myclass{}
m.set(t.Go)
,我的问题是
type testclass struct{}
func (t *testclass) Go(c *Context){
println("Hello");
}
t := &testclass{}
m := &myclass{}
funcName := "Go"
m.set(t.funcName)
什么办法可以做到这一点?
反映?还是什么?
如果不可能,还有其他方法可以做到这一点?
谢谢
你想做什么?你为什么想这样做? – peterSO