Created
October 26, 2022 15:33
-
-
Save mdwhatcott/f42341eef6809dc081d0856535615c3f to your computer and use it in GitHub Desktop.
Index a slice of things in a map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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