Skip to content

Commit

Permalink
Add a program which run func only once regardless of subsequent calls
Browse files Browse the repository at this point in the history
  • Loading branch information
iamniting committed Jun 8, 2023
1 parent 1a6907e commit 0535245
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions goConcepts/sync-once.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"fmt"
"sync"
)

func main() {

var once sync.Once

printHiHello(&once)
printHiHello(&once)
printHiHello(&once)
}

func printHiHello(once *sync.Once) {

// The "Hi" is printed only once, and "Hello" is printed for each call to printHiHello
once.Do(func() {
fmt.Println("Hi")
})
fmt.Println("Hello")
}

0 comments on commit 0535245

Please sign in to comment.