我还比较陌生,我正在寻找番石榴多图index method.的大致等效形式(库或实现),其工作原理如下
它应该执行以下操作:
给出一片结构,构造一个从公用值到共享该值的条目数组的映射。例如:
Repetition struct {
ID int
Days int
Category string
}
reps := []Repetition{
Repetition{ID: 1, Day: 0, Category: "strength"},
Repetition{ID: 2, Day: 0, Category: "aerobic"}
Repetition{ID: 3, Day: 1, Category: "strength"}
Repetition{ID: 4, Day: 1, Category: "aerobic"}
}
result = indexByDay(reps)
结果是:
map[int][]Repetition{
0: []Repetition{Repetition{ID: 1, Day: 0, Category: "strength"}, Repetition{ID: 2, Day: 0, Category: "aerobic"}},
1: []Repetition{Repetition{ID: 3, Day: 1, Category: "strength"}, Repetition{ID: 4, Day: 1, Category: "aerobic"}}
}
问题是,是否有内置或现有的库具有诸如indexByDay之类的方法(某种程度上通用到足以成为库代码?)
很抱歉,如果我的地图文字非常错误。我还是这个语言的新手。
答案 0 :(得分:0)
嗯,事实并非如此:
func group(reps []lifting.Repetition) map[int][]Repetition {
m := make(map[civil.Date][]lifting.Repetition)
for _, rep := range reps {
value, present := m[rep.Day]
if !present {
m[rep.Day] = []lifting.Repetition{rep}
} else {
m[rep.Day] = append(value, rep)
}
}
return m
}