在GO 1.19中,请考虑以下定义:
type A struct{}
type B struct{}
type Field interface {
*A | *B
}
type Person[T Field] struct {
field T
}
假设零值为Field
,我可以动态地创建其潜在价值吗?
func (p Person[T]) do() {
if p.field == nil {
// Can I make a value here that will be *A or *B dynamically depending on T?
// Something that would be equivalent to new(A) or new(B)
p.field = ?
}
}