登录
首页 >  Golang >  Go问答

如何实现通用接口?

来源:stackoverflow

时间:2024-02-12 13:45:22 451浏览 收藏

在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《如何实现通用接口?》,聊聊,希望可以帮助到正在努力赚钱的你。

问题内容

我刚刚看到 go 在其最新版本中合并了泛型,我正在尝试创建一个小项目来了解它是如何工作的。除了现在通用的非常简单的功能之外,我似乎不知道它是如何工作的。我希望能够做这样的事情:

type Dao[RT any] interface {
    FindOne(id string) *RT
}

type MyDao struct {
}

type ReturnType struct {
    id int
}

func (m *MyDao) FindOne(id string) *ReturnType {
    panic("implement me")
}

// how should this look like?

func NewMyDao() *Dao[ReturnType] {
    return &MyDao[ReturnType]{}
}

这可能吗?我似乎没有以这种方式实现接口,并且我尝试了许多相同的组合。

有没有办法实现通用接口?如果不是,是否只能返回 interface{} 类型?


正确答案


类型 *mydao 实现接口 dao[returntype]。因此,该函数应如下所示:

func newmydao() dao[returntype] {
    return &mydao{}
}

请注意,返回类型是通用接口的实例,返回值只是 *mydao 类型的实例。

类型实际上并不实现通用接口,它们实现 You can't use a generic type (including interfaces) without instantiation。从这里开始,它就像前泛型 go 一样,包括带有指针接收器的方法之间的区别。

因此,思考一下如果使用具体类型重写使用类型参数的方法会是什么样子会很有帮助。

让我们考虑一个通用接口和某种类型:

type getter[t any] interface {
    get() t
}

type mystruct struct {
    val string
}

有几种可能的情况

与具体类型参数的接口

实例化为getter[string],通过方法get() string的类型实现

// implements getter[string]
func (m mystruct) get() string {
   return m.val
}

// ok
func foo() getter[string] {
    return mystruct{}
}

以类型参数作为类型参数的接口

具有类型参数的函数可以使用它们来实例化泛型类型,例如getter[t]。实现者必须具有完全相同的 get() t 方法。为了使其有效,它们也是通用的并使用相同的类型参数进行实例化:

所以即使 tstring 也无法编译

// getter[t] literally needs implementors with `get() t` method
func bar[t any]() getter[t] {
    return mystruct{} // doesn't compile, even if t is string
}

使 mystruct 也可以参数化:

type mystruct[t any] struct {
    val t
}

func (m mystruct[t]) get() t {
    return m.val
}

func bar[t any]() getter[t] {
    return mystruct[t]{} // ok
}

具有通用实现者的具体接口

让我们颠倒一下前面的情况。我们保留参数化的 mystruct[t any] 但现在接口未参数化:

type getter interface {
    get() string
}

在这种情况下,mystruct 仅在使用必要的具体类型实例化时才实现 getter

// getter requires method `get() string`
func baz() getter {
    return mystruct[string]{} // instantiate with string, ok
    // return mystruct[int]{} // instantiate with something else, doesn't compile
}

指针接收器

这遵循与上面相同的规则,但需要像往常一样实例化指针类型:

// pointer receiver, implements getter[string]
func (m *mystruct) get() string {
   return m.val
}

func foo() getter[string] {
    return &mystruct{} // ok
    // return mystruct{} // doesn't implement
}

如果 mystruct 是通用的,则效果相同。

// parametrized pointer receiver
func (m *mystruct[t]) get() t {
   return m.val
}

func foo() getter[string] {
    return &mystruct[string]{} // ok
}

因此,在您的情况下,用具体类型替换类型参数的心理练习给出了 dao[returntype] 具有方法 findone(id string) *returntype。实现该方法的类型是*mydao(指针接收器),因此:

func newmydao() dao[returntype] {
    return &mydao{}
}

终于介绍完啦!小伙伴们,这篇关于《如何实现通用接口?》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

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