Go Once

From NovaOrdis Knowledge Base
Revision as of 19:59, 5 September 2023 by Ovidiu (talk | contribs) (→‎Overview)
Jump to navigation Jump to search

Internal

Overview

Once is a Go synchronization construct that is used when a piece of code must be executed only once. This is a useful idiom in initialization.

func init() {
  fmt.Println("initializing")
}

...
var once sync.Once
once.Do(init())

Methods

Do()

All invokers will block, except one, and they will be released after the one that does initialization finishes executing. The others will no execute.