]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add some generic map helpers
authorMatt Joiner <anacrolix@gmail.com>
Wed, 12 Jan 2022 03:20:55 +0000 (14:20 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Sat, 22 Jan 2022 07:43:04 +0000 (18:43 +1100)
generics/map.go [new file with mode: 0644]

diff --git a/generics/map.go b/generics/map.go
new file mode 100644 (file)
index 0000000..23ee5c5
--- /dev/null
@@ -0,0 +1,15 @@
+package generics
+
+func MakeMapIfNilAndSet[K comparable, V any](pm *map[K]V, k K, v V) {
+       m := *pm
+       if m == nil {
+               m = make(map[K]V)
+               *pm = m
+       }
+       m[k] = v
+}
+
+// Does this exist in the maps package?
+func MakeMap[K comparable, V any](pm *map[K]V) {
+       *pm = make(map[K]V)
+}