Created
May 11, 2018 00:39
-
-
Save udhos/51d30efccb51468918a47fd72b3a9e75 to your computer and use it in GitHub Desktop.
sync.Once
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://golang.org/pkg/sync/#Once | |
package main | |
import ( | |
"fmt" | |
"sync" | |
) | |
func main() { | |
var once sync.Once | |
onceBody := func() { | |
fmt.Println("Only once") | |
} | |
done := make(chan bool) | |
for i := 0; i < 10; i++ { | |
go func() { | |
once.Do(onceBody) | |
done <- true | |
}() | |
} | |
for i := 0; i < 10; i++ { | |
<-done | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment