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,我已经能够在没有接收器参数的情况下为方法创建函数映射。这里有一个例子 var funcMap = map[string]func() bool { "&" : And, } func And() bool { return true && false } 但是如果我的函数有一个receiver参数,我怎么能把它添加到函数映射中呢?下面的代码不起作用,但试图显示我想做什么 type Operand struct { Oper

我已经能够在没有接收器参数的情况下为方法创建函数映射。这里有一个例子

var funcMap = map[string]func() bool {
    "&" : And,
}

func And() bool {
    return true && false
}

但是如果我的函数有一个receiver参数,我怎么能把它添加到函数映射中呢?下面的代码不起作用,但试图显示我想做什么

type Operand struct {
    Operator string
    Arg1 interface{}
    Arg2 interface{}
}

var funcMap = map[string]Operand.func() bool {
    "&" : Operand.And,
}

func (o *Operand) And() bool {
    return argToBool(o.Arg1) && argToBool(o.Arg2)
}

我们可以使用函数及其参数创建结构,而不是使用函数创建映射

// Create a function type which takes desired params and return type
type myFunc func(interface{}, interface{})(bool) // Assuming return type as bool

// Create a struct
type workerStruct struct {
     WorkFunc   myFunc
     Arg1       interface{}
     Arg2       interface{}
}
将上述结构与函数和参数一起用作映射的值

funcMap := map[string]workerStruct{}

myStruct := workerStruct {
    WorkFunc : Operand,           // This would your input function
    Arg1     : myarg1,
    Arg2     : myarg2,
}

funcMap["&"] = myStruct

希望这有助于

而不是使用函数创建映射,我们可以使用函数及其参数创建结构

// Create a function type which takes desired params and return type
type myFunc func(interface{}, interface{})(bool) // Assuming return type as bool

// Create a struct
type workerStruct struct {
     WorkFunc   myFunc
     Arg1       interface{}
     Arg2       interface{}
}
将上述结构与函数和参数一起用作映射的值

funcMap := map[string]workerStruct{}

myStruct := workerStruct {
    WorkFunc : Operand,           // This would your input function
    Arg1     : myarg1,
    Arg2     : myarg2,
}

funcMap["&"] = myStruct

希望这有帮助

您只需初始化结构:

o:=操作数{
Arg1:错,
Arg2:是的,
}
var funcMap=map[string]func()bool{
“&”:o.和,
}

您只需初始化结构:

o:=操作数{
Arg1:错,
Arg2:是的,
}
var funcMap=map[string]func()bool{
“&”:o.和,
}

@super我相信这个问题是在问一些与我想做的事情不同的问题。我可以使用的一个选项是使用链接问题答案中提供的反射。然而,我认为我的问题是不同的,kashyap kn提供的答案对于这个用例来说是一个更好的解决方案。我想说的是,这只是一个偏好的问题。要么你有一个signature
func(Operand*)bool的函数,你可以将它包装在一个接口中,要么你有一个包装一个worker func和两个参数的接口。最后,它们基本上是一样的。dupe的优点是,它展示了两种惯用的golang方法来绑定函数和接收器。隐式绑定或转换为以接收方为第一个参数的函数。这两个问题都得到语言核心功能的支持。@super我相信这个问题问的问题与我想做的有点不同。我可以使用的一个选项是使用链接问题答案中提供的反射。然而,我认为我的问题是不同的,kashyap kn提供的答案对于这个用例来说是一个更好的解决方案。我想说的是,这只是一个偏好的问题。要么你有一个signature
func(Operand*)bool的函数,你可以将它包装在一个接口中,要么你有一个包装一个worker func和两个参数的接口。最后,它们基本上是一样的。dupe的优点是,它展示了两种惯用的golang方法来绑定函数和接收器。隐式绑定或转换为以接收方为第一个参数的函数。这两种语言都支持核心功能。