github.com/golodash/godash@v1.3.0/functions/once.go (about) 1 package functions 2 3 import "sync" 4 5 // Creates a function that is restricted to invoking func once. Repeat calls 6 // to the function return the value of the first invocation. 7 // 8 // Complexity: O(1) 9 func Once(function func() []interface{}) func() []interface{} { 10 lock := sync.Mutex{} 11 done := false 12 cache := []interface{}{} 13 return func() []interface{} { 14 lock.Lock() 15 if done { 16 lock.Unlock() 17 } else { 18 done = true 19 cache = function() 20 lock.Unlock() 21 } 22 return cache 23 } 24 }