Mag*_*ama 34 generics interface go
我刚刚看到 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]{}
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?我似乎没有以这种方式实现接口,并且我已经尝试了许多相同的组合。
有没有办法实现通用接口?如果不是,是否只能返回类型interface{}?
bla*_*een 66
类型实际上并不实现通用接口,它们实现通用接口的实例化。没有实例化就不能使用泛型类型(包括接口)。从那里开始,它就像前泛型 Go 一样,包括带有指针接收器的方法之间的差异。
因此,考虑使用具体类型重写使用类型参数的方法会是什么样子会很有帮助。
让我们考虑一个通用接口和某种类型:
type Getter[T any] interface {
Get() T
}
type MyStruct struct {
Val string
}
Run Code Online (Sandbox Code Playgroud)
有几种可能的情况
实例化为Getter[string],通过带有方法的类型实现Get() string
// implements Getter[string]
func (m MyStruct) Get() string {
return m.Val
}
// ok
func foo() Getter[string] {
return MyStruct{}
}
Run Code Online (Sandbox Code Playgroud)
具有类型参数的函数可以使用它们来实例化泛型类型,例如Getter[T]。实现者必须有确切的Get() T方法。为了使其有效,它们也是通用的并使用相同的类型参数进行实例化:
T所以即使是也不会编译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
}
Run Code Online (Sandbox Code Playgroud)
还可以进行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
}
Run Code Online (Sandbox Code Playgroud)
让我们把前面的情况颠倒一下。我们保持参数化MyStruct[T any],但现在接口未参数化:
type Getter interface {
Get() string
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,仅当使用必要的具体类型实例化时才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
}
Run Code Online (Sandbox Code Playgroud)
这遵循与上面相同的规则,但需要像往常一样实例化指针类型:
// 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
}
Run Code Online (Sandbox Code Playgroud)
如果是通用的,也是一样的MyStruct。
// parametrized pointer receiver
func (m *MyStruct[T]) Get() T {
return m.Val
}
func foo() Getter[string] {
return &MyStruct[string]{} // ok
}
Run Code Online (Sandbox Code Playgroud)
因此,在您的情况下,用具体类型替换类型参数的心理练习给出了Dao[ReturnType]has method FindOne(id string) *ReturnType。实现该方法的类型是*MyDao(指针接收者),因此:
func NewMyDao() Dao[ReturnType] {
return &MyDao{}
}
Run Code Online (Sandbox Code Playgroud)
该类型*MyDao实现该接口Dao[ReturnType]。因此,该函数应如下所示:
func NewMyDao() Dao[ReturnType] {
return &MyDao{}
}
Run Code Online (Sandbox Code Playgroud)
请注意,返回类型是通用接口的实例,返回值只是该*MyDao类型的实例。
| 归档时间: |
|
| 查看次数: |
54332 次 |
| 最近记录: |