shad-go/lectures/05-concurrency/synconce/map.go

32 lines
398 B
Go
Raw Normal View History

2020-03-26 13:34:09 +00:00
package synconce
import "sync"
var cache sync.Map
type result struct{}
func do(key string) *result { return new(result) }
type entry struct {
res *result
sync.Once
}
func get(key string) *result {
myEntry := &entry{}
old, loaded := cache.LoadOrStore(key, myEntry)
if loaded {
myEntry = old.(*entry)
}
myEntry.Do(func() {
myEntry.res = do(key)
})
return myEntry.res
}
// OMIT