给定一个通用结构:
type R2[IDTYPE comparable] struct {
ID IDTYPE
IsActive bool}实现接口:
type Storable interface {
Store(ctx context.Context) error}我希望以下定义有效:
func (r R2[int]) Store(ctx context.Context) error {
r.ID = 123 // not allowed
// ...
return nil}但是,方法定义是不允许的。错误是:
'123' (type untyped int) cannot be represented by the type IDTYPE (int)
在 Go 中还不能进行这种通用字段分配吗?
附录:在游乐场上,错误是:
cannot use 123 (untyped int constant) as int value in assignment
并转换为int(123)不起作用。这种情况下的错误是:
cannot use comparable(123) (untyped int constant 123) as int value in assignment
Qyouu
相关分类