Install:
go get github.com/memsql/refcountmap
The idea with refcountmap is that the value for each key is a singleton. When you ask for a value for a specific key, if that key isn't already in the map, then the value is created. If you ask for that same key again, you get another copy of that value. When you're done with a value, you release it. When all copies of the value have been released, the key is removed from the map.
The map is thread-safe.
m := refcountmap.New[string](func() *Thing {
return &Thing{}
})
thing1 := m.Get("hat")
thing2 := m.Get("hat")
// thing1 and thing2 should be pointers to the same Thing