Golang patterns: Difference between revisions
From wikinotes
(Created page with "Some design pattern implementations in go = Singleton = <blockquote> <syntaxhighlight lang="go"> // src: https://medium.com/golang-issue/how-singleton-pattern-works-with-golang-2fdd61cd5a7f var once sync.Once // type global type singleton map[string]string var ( instance singleton ) func NewClass() singleton { once.Do(func() { // <-- atomic, does not allow repeating instance = make(singleton) // <-- thread safe }) return instance } </syntaxhighlight> </blo...") |
|||
Line 4: | Line 4: | ||
<blockquote> | <blockquote> | ||
<syntaxhighlight lang="go"> | <syntaxhighlight lang="go"> | ||
// threadsafe | |||
// src: https://medium.com/golang-issue/how-singleton-pattern-works-with-golang-2fdd61cd5a7f | // src: https://medium.com/golang-issue/how-singleton-pattern-works-with-golang-2fdd61cd5a7f | ||
Latest revision as of 20:18, 1 August 2022
Some design pattern implementations in go
Singleton
// threadsafe // src: https://medium.com/golang-issue/how-singleton-pattern-works-with-golang-2fdd61cd5a7f var once sync.Once // type global type singleton map[string]string var ( instance singleton ) func NewClass() singleton { once.Do(func() { // <-- atomic, does not allow repeating instance = make(singleton) // <-- thread safe }) return instance }