ah, correction: metaSet would have to take m as a pointer to a map.

On Sunday, August 9, 2026 at 6:13:50 PM UTC-3 Jason E. Aten wrote:

> 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/f520d18b-d8dd-4b2e-a2cc-5c10e5bef820n%40googlegroups.com.

Reply via email to