Skip to content

Commit

Permalink
added an example on method to primitive types
Browse files Browse the repository at this point in the history
  • Loading branch information
shijuvar committed Aug 17, 2019
1 parent b7cce8e commit b15baba
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions examples/sync-once/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"fmt"
"sync"
)

var (
doOnce sync.Once
count, singleton int
)

func main() {
Do()
Do()
Do()
}

func Do() {
// 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.
// once.Do(f) is concurrency safe
doOnce.Do(func() {
singleton++
})
count++
fmt.Println("Singleton: ", singleton)
fmt.Println("Count: ", count)
}

0 comments on commit b15baba

Please sign in to comment.