如何在 Go 1.18 中访问泛型结构中的共享字段?我收到错误 "type t has no field or method DATE_START"
How to access shared field in structs in generics in Go 1.18? I get error "type t has no field or method DATE_START"
我有两个具有相同字段名称和类型的结构:
type JOURNAL_TAG struct {
DATE_START time.Time
DATE_END time.Time
ENTRY_NUMBER uint
VALUE float64
}
type INVENTORY_TAG struct {
DATE_START time.Time
DATE_END time.Time
PRICE float64
QUANTITY float64
ACCOUNT_NAME string
}
我有一个访问公共字段 DATE_START
的函数,它应该对这些类型的切片进行排序:
func sort_by_time[t JOURNAL_TAG|INVENTORY_TAG](slice []t, is_ascending bool) {
sort.Slice(slice, func(i, j int) bool {
return slice[i].DATE_START.After(slice[j].DATE_START) == is_ascending
})
}
运行 go build
报编译错误:
slice[i].DATE_START undefined (type t has no field or method DATE_START)
我想用泛型对这两种类型的切片进行排序,可以吗?
我正在使用 go 1.18。
The Go compiler does not support accessing a struct field x.f where x is of type parameter type even if all types in the type parameter's type set have a field f. We may remove this restriction in Go 1.19.
例如,您可以向 returns DATE_START 字段的每个结构添加一个 DateStart() time.Time
方法,然后将该方法用作 type constraint 的一部分如果你想使用泛型。
就是说,您不需要泛型来解决这个特定问题。即使没有泛型,你也可以定义一个接口:
type SomeInterface interface {
DateStart() time.Time
}
然后排序:
items := []SomeInterface{
INVENTORY_TAG{...},
INVENTORY_TAG{...},
}
sort.Slice(items, func(i, j int) bool { return items[i].DateStart().Before(items[j].DateStart()) })
就像@thepudds 所说的那样,在这种情况下最好使用接口实现,但如果你想尝试泛型,你可以这样做:
package main
type JOURNAL_TAG struct {
DATE_START time.Time
DATE_END time.Time
ENTRY_NUMBER uint
VALUE float64
}
type INVENTORY_TAG struct {
DATE_START time.Time
DATE_END time.Time
PRICE float64
QUANTITY float64
ACCOUNT_NAME string
}
type hasDateInterface interface {
DateStart() time.Time
}
func (j JOURNAL_TAG) DateStart(){
return j.DATE_START
}
func (i INVENTORY_TAG) DateStart(){
return i.DATE_START
}
func sort_by_time[t hasDateInterface](slice []t, is_ascending bool) {
sort.Slice(slice, func(i, j int) bool {
return slice[i].DATE_START.After(slice[j].DateStart()) == is_ascending
})
}
我有两个具有相同字段名称和类型的结构:
type JOURNAL_TAG struct {
DATE_START time.Time
DATE_END time.Time
ENTRY_NUMBER uint
VALUE float64
}
type INVENTORY_TAG struct {
DATE_START time.Time
DATE_END time.Time
PRICE float64
QUANTITY float64
ACCOUNT_NAME string
}
我有一个访问公共字段 DATE_START
的函数,它应该对这些类型的切片进行排序:
func sort_by_time[t JOURNAL_TAG|INVENTORY_TAG](slice []t, is_ascending bool) {
sort.Slice(slice, func(i, j int) bool {
return slice[i].DATE_START.After(slice[j].DATE_START) == is_ascending
})
}
运行 go build
报编译错误:
slice[i].DATE_START undefined (type t has no field or method DATE_START)
我想用泛型对这两种类型的切片进行排序,可以吗?
我正在使用 go 1.18。
The Go compiler does not support accessing a struct field x.f where x is of type parameter type even if all types in the type parameter's type set have a field f. We may remove this restriction in Go 1.19.
例如,您可以向 returns DATE_START 字段的每个结构添加一个 DateStart() time.Time
方法,然后将该方法用作 type constraint 的一部分如果你想使用泛型。
就是说,您不需要泛型来解决这个特定问题。即使没有泛型,你也可以定义一个接口:
type SomeInterface interface {
DateStart() time.Time
}
然后排序:
items := []SomeInterface{
INVENTORY_TAG{...},
INVENTORY_TAG{...},
}
sort.Slice(items, func(i, j int) bool { return items[i].DateStart().Before(items[j].DateStart()) })
就像@thepudds 所说的那样,在这种情况下最好使用接口实现,但如果你想尝试泛型,你可以这样做:
package main
type JOURNAL_TAG struct {
DATE_START time.Time
DATE_END time.Time
ENTRY_NUMBER uint
VALUE float64
}
type INVENTORY_TAG struct {
DATE_START time.Time
DATE_END time.Time
PRICE float64
QUANTITY float64
ACCOUNT_NAME string
}
type hasDateInterface interface {
DateStart() time.Time
}
func (j JOURNAL_TAG) DateStart(){
return j.DATE_START
}
func (i INVENTORY_TAG) DateStart(){
return i.DATE_START
}
func sort_by_time[t hasDateInterface](slice []t, is_ascending bool) {
sort.Slice(slice, func(i, j int) bool {
return slice[i].DATE_START.After(slice[j].DateStart()) == is_ascending
})
}