[Golang (Go)] Use sync.Once to execute only the first call
sync.Once
Once is an object that will perform exactly one action.
.Do calls the function f if and only if .Do is being called for the first time for this instance of Once. In other words, given var once Once if once.Do(f) is called multiple times, only the first call will invoke f,
even if f has a different value in each invocation.
A new instance of Once is required for each function to execute.
Examples
1 | // main.go |
1 | go run main.go |
References
[1] Once | sync - The Go Programming Language - https://golang.org/pkg/sync/#example_Once
[3] src/sync/once.go - The Go Programming Language - https://golang.org/src/sync/once.go