Context
You have applied the Microservice architecture pattern and architected your system as a set of services. Each service is deployed as a set of service instances for throughput and availability.
...You have applied the Microservice architecture pattern and architected your system as a set of services. Each service is deployed as a set of service instances for throughput and availability.
...Refer to https://swsmile.info/post/design-pattern-singleton-pattern/ for Singleton pattern.
init()
functionsPackage init()
functions are guaranteed to be called only once and all called from a single thread ( they’re thread-safe unless you make them multi-threaded). But that makes you dependent on boot order. And you should not write codes in an *init ( )* that you need a guarantee of execution at any given time
type A struct {
str string
}
var singleton *A
func init() {
//initialize static instance on load
singleton = &A{str: "abc"}
}
//GetInstanceA - get singleton instance pre-initialized
func GetInstanceA() *A {
return singleton
}