Data Access

Protect private data by allowing external users to populate it via constructor options only.

type data struct {
    password string
}

type Object struct {
    private *data
}

func WithPassword(password string) Option {
    return func(instance *Object) error {
        if instance.private == nil {
            instance.private = &data{}    
        }
    
        instance.private.password = password
        return nil    
    }
}

If the data needs to be read by an external user, provide a method passing the value of the data. Do not allow pointers to escape.

It should be clear reading this that there may be questions about what you're doing with the underlying data, and whether you really want to expose it externally or whether the package should handle the work being done with the values involved.

Truly public information should be exposed as such.

Last updated