如何实现通用接口?
我刚刚看到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]{}
}
这甚至可能吗?我似乎并没有以这种方式实现界面,并且我尝试了许多相同的组合。
有没有办法实现通用接口?如果不是,是否仅返回接口{}
类型?
I've just seen Go has incorporated generics in its latest release, and I'm trying to create a small project to understand how it works. I don't seem to figure out how it works apart from very simple functions being now generic. I'd like to be able to do things like this:
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]{}
}
Is that even possible? I don't seem to be implementing the interface that way, and I've tried many combinations of the same.
Is there a way to implement a generic interface? If not, is the alternative only to return the interface{}
type?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
类型实际上并不能实现通用接口,而是实现通用接口的实例。 您无法使用通用类型(包括Interfaces)没有实例化。从那里开始,就像前生物一样,包括用指针接收器的方法之间的区别。
因此,考虑使用类型参数的方法,如果您使用混凝土类型重写,使用类型参数的方法会很有帮助。
让我们考虑一个通用接口和某种类型:
有一些可能的情况
与混凝土类型参数
接口,将其实例化为
getter [string]
,由使用方法get(get()字符串
接口 实现。使用类型参数作为类型
参数的类型参数函数,可以使用这些函数来实例化通用类型,例如
getter [t]
。实施者必须完全具有get()t
方法。为了使其有效,它们也是通用的,并使用相同类型的参数实例化:因此,即使
t
是String
制作
MyStruct
MyStruct 还参数化作品:带有通用实现者的混凝土接口,
让我们逆转以前的情况。我们将参数化
mystruct [thony]
保留使用必要的具体类型:
指针接收器
遵循与上述相同的规则,但需要像往常一样实例化指针类型:
如果
myStruct
是通用的,则相同。因此,在您的情况下,用具体类型替换类型参数的心理练习使
dao [returnType]
具有方法findOne(ID字符串) *returnType
。因此,实现此方法的类型是*myDao
(指针接收器),因此: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:
There's a few possible cases
Interface with concrete type argument
Instantiate as
Getter[string]
, implemented by types with methodGet() string
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 theGet() 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
isstring
Making
MyStruct
also parametrized works: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:In this case,
MyStruct
implementsGetter
only when it is instantiated with the necessary concrete type:Pointer receivers
This follows the same rules as above, but requires instantiating pointer types, as usual:
and it is the same if
MyStruct
is generic.So in your case, the mental exercise of replacing the type params with concrete types gives that
Dao[ReturnType]
has methodFindOne(id string) *ReturnType
. The type that implements this method is*MyDao
(pointer receiver), therefore:类型
*mydao
实现接口dao [returnType]
。因此,该函数应该看起来像:请注意,返回类型是通用接口的实例,返回值只是
*myDao
类型的实例。The type
*MyDao
implements the interfaceDao[ReturnType]
. Thus, the function should look like:Note that the return type is an instance of the generic interface, and the returned value is simply an instance of the
*MyDao
type.