How to use sync Once

Use sync.Once with the Do method to ensure a function runs exactly once across concurrent goroutines.

Use the sync.Once type to ensure a function executes exactly once across multiple goroutines.

var once sync.Once
var result string

func init() {
    once.Do(func() {
        result = "initialized"
    })
}

The Do method takes a function and runs it the first time it is called; subsequent calls return immediately without re-executing the function.