Skip to content

Instantly share code, notes, and snippets.

@meson10 meson10/syncOnce.go
Last active Jun 29, 2016

What would you like to do?
package main
import (
type MyStruct struct {
field int
var onceCached *MyStruct
func initMyStruct() MyStruct {
return MyStruct{field: 1}
var once sync.Once
func getIntOnce(ident int) *MyStruct {
log.Println("No lock", ident)
once.Do(func() {
log.Println("Initializing GetInt")
x := initMyStruct()
onceCached = &x
log.Println("No lock return", ident)
return onceCached
func main() {
var wg sync.WaitGroup
for x := range []int{0, 1, 2, 3, 4, 5} {
go func(ident int) {
defer wg.Done()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.