Below is my golang code. Each time validate method is called my compile method gets executed. I want to compile only once, not each time we call validate.
1) How to do it ? 2) My idea was to create an instance variable which would be nil at start. It would be lazy initialized in validate.
if (a != nil) {
a, err := regexp.Compile(rras.Cfg.WhiteList)
}
However if I declare a variable as an instance variable,
var a *Regexp; // regexp.Compile returns *Regexp
my compiler underlines in red. How to fix it ?
type RRAS struct {
Cfg *RRAPIConfig
}
type RRAPIConfig struct {
WhiteList string
}
func (rras *RRAS) validate(ctx context.Context) error {
a, err := regexp.Compile(rras.Cfg.WhiteList)
}