I have two structs that have the some of the same field names and types:
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
}
and I have a func that accesses the common field DATE_START
that should sort the slices of these types:
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
})
}
Running go build
reports a compiler error:
slice[i].DATE_START undefined (type t has no field or method DATE_START)
I want to sort the slices of these two types using generics, is it possible?
I am using go 1.18.