Switch from atomic.Value to sync.RWMutex for backward compatibility
diff --git a/homedir.go b/homedir.go
index 2f54ed6..c4c668b 100644
--- a/homedir.go
+++ b/homedir.go
@@ -8,14 +8,15 @@
"path/filepath"
"runtime"
"strings"
- "sync/atomic"
+ "sync"
)
// DisableCache will disable caching of the home directory. Caching is enabled
// by default.
var DisableCache bool
-var homedirCache atomic.Value
+var homedirCache string
+var cacheLock sync.RWMutex
// Dir returns the home directory for the executing user.
//
@@ -23,9 +24,11 @@
// An error is returned if a home directory cannot be detected.
func Dir() (string, error) {
if !DisableCache {
- cached := homedirCache.Load()
- if cached != nil && cached != "" {
- return cached.(string), nil
+ cacheLock.RLock()
+ cached := homedirCache
+ cacheLock.RUnlock()
+ if cached != "" {
+ return cached, nil
}
}
@@ -41,8 +44,9 @@
if err != nil {
return "", err
}
-
- homedirCache.Store(result)
+ cacheLock.Lock()
+ homedirCache = result
+ cacheLock.Unlock()
return result, nil
}