Go Once: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * The <tt>sync</tt> Package * Goroutines | Synchronization =Overview= <code>Once</code> is a Go synchr...")
 
Line 4: Line 4:
=Overview=
=Overview=
<code>Once</code> 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.
<code>Once</code> 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.
<syntaxhighlight lang='go'>
var once sync.Once
once.Do(
</syntaxhighlight>
=Methods=
==<tt>Do()</tt>==
All invokers will block, except one, and they will be released after the one that does initialization finishes executing. The others will no execute.

Revision as of 19:58, 5 September 2023

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.

var once sync.Once
once.Do(

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.