Skip to content

Instantly share code, notes, and snippets.

@neomantra
Created March 31, 2023 13:31
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 neomantra/f1ab990de6b5a9840a9f9dde46f6a7e9 to your computer and use it in GitHub Desktop.
Save neomantra/f1ab990de6b5a9840a9f9dde46f6a7e9 to your computer and use it in GitHub Desktop.
NewPointerValueMap converts a map[string]*T to a map[string]T, omitting nil pointers.
// NewPointerValueMap converts a map[string]*T to a map[string]T, omitting nil pointers.
func NewPointerValueMap[K comparable, V any](src map[K]*V) map[K]V {
dst := make(map[K]V)
for k, ptr := range src {
if ptr != nil {
dst[k] = *ptr
}
}
return dst
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment