From 14fe621ee60d9b527d9ec6b26239009987bbc10b Mon Sep 17 00:00:00 2001 From: francoismichel Date: Mon, 29 Jan 2024 18:20:12 +0100 Subject: [PATCH] make generic SyncMap with with actually any type (#116) The SyncMap genreic type does not work with non-nilable types, as the nil value returned by sync.Map cannot be converted to the V generic type. Replace nil by the zero value if that's the case. --- util/util.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/util/util.go b/util/util.go index 3636670..d163dfe 100644 --- a/util/util.go +++ b/util/util.go @@ -335,6 +335,11 @@ func NewSyncMap[K comparable, V any]() SyncMap[K, V] { func (m *SyncMap[K, V]) Get(key K) (V, bool) { val, ok := m.inner.Load(key) + if val == nil { + // we can't return nil for *any* type, so we create a zero value for the type and return it, instead of nil + var zero V + return zero, ok + } return val.(V), ok }