简体   繁体   English

如何将接口转换为它实现的另一个接口?

[英]How to convert interface to another interface which it implements?

In short - I would like to be able to cast an interface type whose underlying type implements a specific interface to that specific interface.简而言之- 我希望能够将其基础类型实现特定接口的接口类型转换为该特定接口。

I am using the plugin package to lookup a New function which looks like so (I have many others the same):我正在使用插件 package 查找新的 function,它看起来像这样(我有很多其他的相同):

func NewDomainPrimaryKey() any { return DomainPrimaryKey{} }

(This is generated at run-time so I can't just reference it as DomainPrimaryKey) (这是在运行时生成的,所以我不能将它作为 DomainPrimaryKey 引用)

My lookup and call is like so:我的查找和调用是这样的:

                plugin, err := plugin.Open("my-plugin")
                if err != nil {
                    return err
                }

                symGet, err := plugin.Lookup("New" + pluginName)
                if err != nil {
                    return err
                }

                newGenModel, ok := symGet.(func() any)
                if !ok {
                    return errors.New("unexpected type from module symbol")
                }

                anyGenModel := newGenModel()
                genModel, ok := anyGenModel.(GenModel) // **this is where the problem is
                if !ok {
                    return errors.New("unexpected type from module symbol")
                }

                genModelInstance := genModel.Get()

In the above I am trying to cast 'anyGenModel' (an interface) to the 'GenModel' interface which it implements, however, this doesn't work.在上面,我试图将“anyGenModel”(一个接口)转换为它实现的“GenModel”接口,但是,这不起作用。

I am certain it implements this interface because when I do the following, I get no errors.我确定它实现了这个接口,因为当我执行以下操作时,我没有收到任何错误。

type GenModel interface {
    Get() any
    TableName() string
}

var _ GenModel = (*DomainPrimaryKey)(nil) // this doesn't complain

How can I do this?我怎样才能做到这一点? I found this article which I don't think is what I am looking for but seems similar.我发现这篇文章我认为不是我要找的,但看起来很相似。

Thanks in advance for any help on this - this has become a real blocker for me.预先感谢您对此提供的任何帮助 - 这对我来说已经成为一个真正的障碍。

If the underlying type implements both interfaces, this is very straightforward:如果底层类型实现了这两个接口,这就非常简单了:

package main

import "fmt"

type IFace1 interface {
    DoThis()
}

type IFace2 interface {
    DoThat()
}

type impl struct{}

func (i *impl) DoThis() {
    fmt.Println("I implement IFace1")
}

func (i *impl) DoThat() {
    fmt.Println("I implement IFace2")
}

func GetIFace1() IFace1 {
    return &impl{}
}

func main() {
    i1 := GetIFace1()

    i1.DoThis()

    i2 := i1.(IFace2)

    i2.DoThat()
}

playground操场

If your code is not working, then I would begin by questioning your assertion that the underlying type of anyGenModel actually implements GenModel and work from there.如果您的代码无法正常工作,那么我会首先质疑您关于anyGenModel的基础类型实际上实现GenModel并从那里开始工作的断言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM