登录
首页 >  Golang >  Go问答

具有结构指针返回值的 func 与具有接口指针返回值的 func 不匹配。可以以某种方式编译吗?

来源:stackoverflow

时间:2024-02-20 09:15:55 461浏览 收藏

偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《具有结构指针返回值的 func 与具有接口指针返回值的 func 不匹配。可以以某种方式编译吗?》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!

问题内容

有一些生成的代码我无法更改。它们的一般结构如下:

// these structures & methods i cannot change

type notmystruct struct {
    embeddedcaller
}
type embeddedcaller struct {
    foobar string
}

func (_embeddedcaller *embeddedcaller) dostuff() string {
    return "abc"
}

func newnotmystruct() *notmystruct {
    return ¬mystruct{
        embeddedcaller{"blah"},
    }
}

生成代码的一般模式是 1) 父结构 + 嵌入结构 2) 嵌入结构上的方法和 3) 创建结构的新方法。

我有许多生成的“合同”,它们都有不同的类型,即 notmystruct1 notmystruct2 等。嵌入的结构也都是不同的类型,即 embeddedcaller1、embeddedcaller2 等。

但是它们都有相同的方法 dostuff 和相同的返回值。我想做的是创建一些 id 到 new 函数的映射,然后迭代每个函数并调用 dostuff 方法。但是我的代码无法编译。如果编译它会看起来像这样:

type MyDoStuffInterface interface {
    DoStuff() string
}
var instantiations map[string]func()*MyDoStuffInterface{
    "1": NewNotMyStruct, //<-- does not compile here because *MyDoStuffInterface doesn't match *NotMyStruct
   ...
}

for id, instantiationFunc := range instantiations {
    instance := instantiationFunc()
    instance.DoStuff()
}

有可能做我想做的事吗?如果是这样怎么办?如果没有,保持干燥的最简单方法是什么?


解决方案


首先,您需要将 *mydostuffinterface 替换为 mydostuffinterface。接口指针确实有其用途,但几乎所有时候它们都不需要(或正确)。

其次,函数的类型 (func()*notmystruct) 与 func()mydostuffinterface 不匹配。 (比我更有经验的人可能会说 go 中的函数类型不是协变的或类似的东西)。

解决第二个问题的最佳方法是使用具有正确类型的包装函数。 (另一种方法是避免使用类型系统,并使用 interface{} 作为您的函数类型,并使用运行时反射来调用您的函数)。

这是一个完整的编译示例 (playground link)。(我不得不稍微更改一下您的 instantiations 变量,因为初始化地图的语法不正确。)

package main

type notmystruct struct {
    embeddedcaller
}
type embeddedcaller struct {
    foobar string
}

func (_embeddedcaller *embeddedcaller) dostuff() string {
    return "abc"
}

func newnotmystruct() *notmystruct {
    return ¬mystruct{
        embeddedcaller{"blah"},
    }
}

type mydostuffinterface interface {
    dostuff() string
}

func main() {
    var instantiations = map[string](func() mydostuffinterface){
        "1": func() mydostuffinterface { return newnotmystruct() },
    }

    for _, instantiationfunc := range instantiations {
        instance := instantiationfunc()
        instance.dostuff()
    }
}

使用以下地图:

var instantiations = map[string]func()MyDoStuffInterface{
    "1": func() MyDoStuffInterface  { 
           return NewNotMyStruct() 
         },
}

一些注意事项:

  • 需要匿名“适配器”函数,因为 newnotmystruct() 返回 *notmystruct,而不是 mydostuffinterface

  • 不要使用指向接口的指针。不需要它们。

Run it on the Go Playground

好了,本文到此结束,带大家了解了《具有结构指针返回值的 func 与具有接口指针返回值的 func 不匹配。可以以某种方式编译吗?》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>