可以使用 Go 语言的 map 数据类型来分组数据。首先,你需要指定 map 的 key 类型,然后将结构体的某个字段的值作为 map 的 key,将结构体作为 map 的 value。
例如,有如下结构体:
type Person struct { ID int Name string Age int }
你可以使用 Age 字段来分组数据,代码如下:
personsByAge := make(map[int][]Person) for _, p := range persons { personsByAge[p.Age] = append(perso