3 ms·
That's not exactly what you mean, but you could do this in Go by using the `init()` function. From there you assign a package level variable to either an object
by dgellow 6y ago
That's not exactly what you mean, but you could do this in Go by using the `init()` function. From there you assign a package level variable to either an object that only exposes public getters, or one that is only exposed/manipulated by public functions.
var myVar MyType
func init() {
myVar = newMyType()
}
func GetMyType() MyType {
return myVar
}
init() is run when the package is imported.
myVar is constant (though not guaranteed at the type level).
GetMyType() is the only way to access myVar and return a copy, not a reference.