On Sunday, August 9, 2026 at 1:13:14 PM UTC-3 Tom Limoncelli wrote:

For example, in github.com/DNSControl/dnscontrol every RecordConfig{} 
(the struct that stores a DNS record) includes a field Metadata: 
map[string]string for storing key/value pairs. Few of the records will 
ever store metadata. We don't want to preallocate potentially millions 
of empty map structures. However it is annoying to have to check for 
nil in the code that does use this feature.


The tension here between "wanting to save alot of space" and "checking if 
space has been saved" 
seems fundamental. These are two sides of the same coin. 

Your best bet is to simply encapsulate access with getter and setter 
helper functions that do the nil checks for you, then use the helpers 
everywhere.
This is simple and straight forward.

func metaGet(m map[string]string, key string) (val string, ok bool) {
   if m == nil {
       return
   }
   val, ok = m[key]
   return
}

func metaSet(m map[string]string, key string, val string) {
    if m == nil {
         m = make(map[string]string)
    }
   m[key] = val
}

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/20958375-d539-4174-a43e-6b4cfaa08f7en%40googlegroups.com.

Reply via email to