As @Brenden explained, this can be good practice when some “attributes” remain “private”.
I use the words, constructor and private event attributes, although Go does not support real OOP, but here the goal is to emulate some OOP functions.
One way to achieve this without linter claims is to use the exported structure to encapsulate an unexported type. But then you need a “constructor”, because you cannot create an unexported type somewhere else.
Example:
A counter that can only increase. So you want to hide the meaning and keep it secret.
package counter type Counter struct { inner counter } type counter struct { value int } func New() Counter { return Counter{counter{0}} } func (obj *Counter) Increment() { obj.inner.value++ } func (obj Counter) Value() int { return obj.inner.value }
Sylvain willy
source share