dictionary - 具有接收者函数回调的 Map 语法

标签 dictionary go

是否可以在 golang 中创建一个包含具有接收器的函数的映射?

我要完成以下任务

函数回调:

func (my *mystruct) doSometing(int parameter1){
// do something
}

func (my *mystruct) doAnotherThing(int parameter1){
// do something
}

包含指向函数的指针的映射

var lookupMap = map[string]func(int){
    "action1" : doSomething,
    "action2" : doAnotherThing
}

不幸的是,这不起作用,因为回调函数绑定(bind)到接收器。 Go 编译器说:

"undefined doSomething"

我的问题:

创建值是绑定(bind)到特定接收器的函数的映射的语法是什么?

类似于(伪代码):

var lookupMap = map[string]((*mystruct)func(int)){
}

感谢任何帮助!

最佳答案

有方法值

您可以使用 Method values以此目的。方法值是具有隐式接收者的函数值。引自 Spec: Method values :

If the expression x has static type T and M is in the method set of type T, x.M is called a method value.

所以方法值的语法是x.M,例如x是类型的值,M是名称方法。这导致函数具有与没有接收器的方法相同的参数(和返回类型),因为接收器将与方法值一起保存并且是隐式的。

所以这意味着为您的 doSometing()doAnotherThing() 方法存储方法值,函数类型将只是 func (int)(无接收器)。

这是一个工作示例:

type mystruct struct {
    name string
}

func (my *mystruct) doA(i int) {
    fmt.Printf("[doA]: I'm %s, param is: %d\n", my.name, i)
}

func (my *mystruct) doB(i int) {
    fmt.Printf("[doB]: I'm %s, param is: %d\n", my.name, i)
}

func main() {
    my1 := &mystruct{"Bob"}
    my2 := &mystruct{"Alice"}
    lookupMap := map[string]func(int){
        "action1": my1.doA,
        "action2": my2.doB,
    }

    lookupMap["action1"](11)
    lookupMap["action2"](22)
}

输出(在 Go Playground 上尝试):

[doA]: I'm Bob, param is: 11
[doB]: I'm Alice, param is: 22

使用方法表达式

如果您不想将接收器保存在字典中(在方法值中),您可以使用 Method expressions .

不同的是,获取函数值时,不是使用x.M,而是使用T.M,会得到一个函数值,函数类型为相同的参数(和返回类型),但接收者类型也将在参数列表中,并且在第一位。参见 Spec: Method expressions 的引述:

If M is in the method set of type T, T.M is a function that is callable as a regular function with the same arguments as M prefixed by an additional argument that is the receiver of the method.

因此,在您的情况下,要使用的函数类型将如下所示:func(*mystruct, int)

此外,由于不会保存接收者,因此您必须在调用这些函数时提供它。

查看这个工作示例(它是第一个示例的修改):

type mystruct struct {
    name string
}

func (my *mystruct) doA(i int) {
    fmt.Printf("[doA]: I'm %s, param is: %d\n", my.name, i)
}

func (my *mystruct) doB(i int) {
    fmt.Printf("[doB]: I'm %s, param is: %d\n", my.name, i)
}

func main() {
    lookupMap := map[string]func(*mystruct, int){
        "action1": (*mystruct).doA,
        "action2": (*mystruct).doB,
    }

    my1 := &mystruct{"Bob"}
    my2 := &mystruct{"Alice"}
    lookupMap["action1"](my1, 11)
    lookupMap["action2"](my2, 22)
}

输出相同(在 Go Playground 上尝试):

[doA]: I'm Bob, param is: 11
[doB]: I'm Alice, param is: 22

查看类似问题:

golang - pass method to function

golang function alias on method receiver

关于dictionary - 具有接收者函数回调的 Map 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39936099/

相关文章:

python - 映射列表 - python 中的列表作为字典

go - 带有错误的Golang错误包装/解包&&类型检查。is()

go - glide 和 godep 之间的主要区别是什么?

python - Django:使用对象作为字典键是否合理?

python - 使用 DictReader 从 CSV 文件读取到列表中

c++ - 如果我不使用异常,如何检查是否可以创建 map ?

windows - Golang HTTP x509 : certificate signed by unknown authority error

go - json.Unmarshal 在进行 curl 调用时不填充结构

google-app-engine - Appengine > Go > 映射数据存储结果

dictionary - 戈朗 : group and sum slice of structs