1 回答

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個(gè)贊
這將創(chuàng)建 SumRecord 到整數(shù)的新映射,表示該特定類型/年份分組的出現(xiàn)次數(shù)總和。
type Record struct {
? ? UID? int
? ? Type string
? ? Year string
}
type SumRecord struct {
? ? Type string
? ? Year string
}
m := make(map[int]Record)
// e.g. [{"1996","non-fiction"}:4], representing 4 occurrences of {"1996","non-fiction"}
srMap := make(map[SumRecord]int)
// add records
// loop over records
for key := range m {
? ? sr := SumRecord{
? ? ? ? Type: m[key].Type,
? ? ? ? Year: m[key].Year,
? ? }
? ? // creates new counter or increments existing pair counter by 1
? ? srMap[sr] += 1
}
// print all mappings
fmt.Println(srMap)
// specific example
fmt.Println(srMap[SumRecord{
? ? Year: "1996",
? ? Type: "non-fiction",
}])
- 1 回答
- 0 關(guān)注
- 138 瀏覽
添加回答
舉報(bào)