Skip to content

Instantly share code, notes, and snippets.

@mdwhatcott
Created October 26, 2022 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdwhatcott/f42341eef6809dc081d0856535615c3f to your computer and use it in GitHub Desktop.
Save mdwhatcott/f42341eef6809dc081d0856535615c3f to your computer and use it in GitHub Desktop.
Index a slice of things in a map
func index[K comparable, V any](list []V, key func(V) K) map[K]V {
result := make(map[K]V)
for _, value := range list {
result[key(value)] = value
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment