Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

泛型切片分组

group

func GroupBy[T any, U comparable](collection []T, iteratee func(T) U) map[U][]T {
 result := map[U][]T{}

 for _, item := range collection {
  key := iteratee(item)

  result[key] = append(result[key], item)
 }

 return result
}