June 2017
Intermediate to advanced
400 pages
10h 32m
English
A common pattern for these global package-level objects is to keep the global unexported and expose only the functionality desired via methods. Typically, you could also include a method to return a copy of the global logger for packages that want a logger object.
The sync.Once type is a newly introduced structure. This structure, in conjunction with the Do method, will only execute in the code once. We use this in our initialization code, and the Init function will throw an error if Init is called more than once.
Although this example uses a log, you can also imagine cases where this might be useful with a database connection, data streams, and a number of other use cases.
Read now
Unlock full access