我刚刚看到Go在其最新版本中加入了泛型,我正在try 创建一个小项目来了解它是如何工作的.除了现在非常简单的通用函数之外,我似乎不知道它是如何工作的.我希望能够做到这样:
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]{}
}
这可能吗?我似乎没有以这种方式实现接口,我try 了许多相同的组合.
有没有实现通用接口的方法?如果不是,是否只返回interface{}
类型?
谢谢.