美文网首页
Golang中怎样实现一个泛型的接口

Golang中怎样实现一个泛型的接口

作者: cnwinds | 来源:发表于2022-10-25 09:35 被阅读0次

(https://stackoverflow.com/posts/72050933/timeline)

Types don't actually implement generic interfaces, they implement instantiations of generic interfaces. You can't use a generic type (including interfaces) without instantiation. From there, it is just like pre-generics Go, including the difference between methods with pointer receiver.

Therefore it is helpful to think what the methods that use type parameters would look like if you rewrote them with concrete types.

Let's consider a generic interface and some type:

type Getter[T any] interface {
    Get() T
}

type MyStruct struct {
    Val string
}

There's a few possible cases

Interface with concrete type argument

Instantiate as Getter[string], implemented by types with method Get() string

// implements Getter[string]
func (m MyStruct) Get() string {
   return m.Val
}

// ok
func foo() Getter[string] {
    return MyStruct{}
}

Interface with type parameter as type argument

Functions that have type parameters may use those to instantiate generic types, e.g. Getter[T]. Implementors must have exactly the Get() T method. For that to be valid, they are also generic and instantiated with the same type parameter:

So this doesn't compile even if T is string

// 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
}

Making MyStruct also parametrized works:

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
}

Concrete interface with generic implementor

Let's reverse the previous cases. We keep the parametrized MyStruct[T any] but now the interface is not parametrized:

type Getter interface {
    Get() string
}

In this case, MyStruct implements Getter only when it is instantiated with the necessary concrete type:

// 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 receivers

This follows the same rules as above, but requires instantiating pointer types, as usual:

// 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
}

and it is the same if MyStruct is generic.

// parametrized pointer receiver
func (m *MyStruct[T]) Get() T {
   return m.Val
}

func foo() Getter[string] {
    return &MyStruct[string]{} // ok
}


So in your case, the mental exercise of replacing the type params with concrete types gives that Dao[ReturnType] has method FindOne(id string) *ReturnType. The type that implements this method is *MyDao (pointer receiver), therefore:

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

相关文章

  • Golang学习笔记---“泛型”的实现

    Golang学习笔记---“泛型”的实现 大家都知道,go语言中是没有泛型的,但是我们可以通过空接口的方式来实现泛...

  • Golang中怎样实现一个泛型的接口

    (https://stackoverflow.com/posts/72050933/timeline[https:...

  • Java 19-5.1泛型

    泛型类定义泛型类可以规定传入对象 泛型类 和泛型方法 泛型接口 如果实现类也无法确定泛型 可以在继承类中确定泛型:

  • 008-自定义泛型,Collections

    自定义泛型 泛型类 代码实现 测试 泛型接口 代码实现 泛型方法 代码演示 测试 泛型上下边界 Collectio...

  • Golang 使用接口实现泛型

    在C/C++中我们可以使用泛型的方法使代码得以重复使用,最常见例如stl functions:vector vi...

  • Collection接口&List接口简介

    Collection接口: List接口及其实现类--------ArrayList 泛型:

  • Java 集合深度复制

    泛型对象实现 Cloneable 接口 重写泛型对象的 clone 方法 使用的时候调用 泛型对象的 clone...

  • golang泛型前瞻

    9月更新了golang设计草稿中的泛型语法这里可以在线编译如下 这是一个泛型的Print函数 循环打印泛型切片中的...

  • Java泛型

    泛型有三种实现方式,分别是泛型接口、泛型类、泛型方法。下面通过泛型方法来介绍什么是类型参数。 泛型方法声明方式:访...

  • 泛型接口的概述和使用 implements

    泛型接口的概述和使用 implements implements是一个类,实现一个接口用的关键字,它是用来实现接口...

网友评论

      本文标题:Golang中怎样实现一个泛型的接口

      本文链接:https://www.haomeiwen.com/subject/yvpnzrtx.html