我试图在 Go 中定义一个通用函数,它接受具有特定字段的值,例如 ID int
。我尝试了几种方法,但似乎都不起作用。这是我尝试过的示例。
package main
import (
"fmt"
)
func Print[T IDer](s T) {
fmt.Print(s.ID)
}
func main() {
Print(Person{3, "Test"})
}
type IDer interface {
~struct{ ID int }
}
type Person struct {
ID int
Name string
}
type Store struct {
ID int
Domain string
}
这是 Playground 链接:https://gotipplay.golang.org/p/2I4RsUCwagF
在上面的示例中,我想保证传递给 Print
函数的每个值都有一个属性 ID int
,该属性也可以在函数中访问。有什么方法可以实现 Go 而无需在接口(interface)中定义方法(例如,GetID() int
)?
最佳答案
Is there any way I can achieve this is Go without defining a method in an interface (e.g., GetID() int)?
不,您必须在接口(interface)中定义方法。
Go 1.18 中的泛型实现不支持结构类型,尽管最初的类型参数提案建议支持。如需访问联合中的公共(public)字段,另请参阅 this explanation .
尽管如此,我认为有必要指出一个很容易从您的示例中产生的误解:近似值 ~T
(代字号类型)的含义是“底层类型的集合”类型是 T。
现在,当你写:
~struct{ ID int }
这意味着类型的基础类型是完全 struct{ ID int }
。无论如何,这不包括具有字段 ID int
和 的结构。例如。 的底层类型 type Foo struct { ID int;名称字符串 }
是 struct { ID int;命名字符串 }
,而不是 struct{ ID int }
,因此无论如何都不会满足约束条件。
当前时间参数实现没有指定部分结构类型的语法。我记得一个proposal在接口(interface)约束中添加字段项(连同类型项和方法),在线的一些东西:
type IDer interface {
ID int
}
这将在不破坏代字号 ~
含义的情况下启用您正在尝试做的事情。但这不会包含在 Go 1.18 中。
关于go - 有没有一种方法可以使用泛型确保传递的值具有某些字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71365287/