feat: add GetOrSet

This commit is contained in:
dudaodong committed 2024-06-24 17:28:51 +08:00
1 parent aeef0418a4
commit a220220f09
5 files changed
+122

No files matched your search

+12
View File
@@ -436,3 +436,15 @@ func ToSortedSlicesWithComparator[K comparable, V any](m map[K]V, comparator fun
return keys, sortedValues
}
// GetOrSet returns value of the given key or set the given value value if not present
// Play: todo
func GetOrSet[K comparable, V any](m map[K]V, key K, value V) V {
if v, ok := m[key]; ok {
return v
}
m[key] = value
return value
}