"cmd/go/internal/str"
)
-var cwd string
-var cwdOnce sync.Once
-
// UncachedCwd returns the current working directory.
// Most callers should use Cwd, which caches the result for future use.
// UncachedCwd is appropriate to call early in program startup before flag parsing,
return wd
}
+var cwdOnce = sync.OnceValue(UncachedCwd)
+
// Cwd returns the current working directory at the time of the first call.
func Cwd() string {
- cwdOnce.Do(func() {
- cwd = UncachedCwd()
- })
- return cwd
+ return cwdOnce()
}
// ShortPath returns an absolute or relative name for path, whatever is shorter.
}()
}
-var onceProcessSignals sync.Once
+var processSignalsOnce = sync.OnceFunc(processSignals)
// StartSigHandlers starts the signal handlers.
func StartSigHandlers() {
- onceProcessSignals.Do(processSignals)
+ processSignalsOnce()
}
// Default returns the default cache to use.
// It never returns nil.
func Default() Cache {
- defaultOnce.Do(initDefaultCache)
- return defaultCache
+ return initDefaultCacheOnce()
}
-var (
- defaultOnce sync.Once
- defaultCache Cache
-)
+var initDefaultCacheOnce = sync.OnceValue(initDefaultCache)
// cacheREADME is a message stored in a README in the cache directory.
// Because the cache lives outside the normal Go trees, we leave the
// initDefaultCache does the work of finding the default cache
// the first time Default is called.
-func initDefaultCache() {
+func initDefaultCache() Cache {
dir, _ := DefaultDir()
if dir == "off" {
if defaultDirErr != nil {
}
if v := cfg.Getenv("GOCACHEPROG"); v != "" && goexperiment.CacheProg {
- defaultCache = startCacheProg(v, diskCache)
- } else {
- defaultCache = diskCache
+ return startCacheProg(v, diskCache)
}
+
+ return diskCache
}
var (
"sync"
)
-var (
- tags map[string]bool
- tagsOnce sync.Once
-)
-
// Tags returns a set of build tags that are true for the target platform.
// It includes GOOS, GOARCH, the compiler, possibly "cgo",
// release tags like "go1.13", and user-specified build tags.
func Tags() map[string]bool {
- tagsOnce.Do(func() {
- tags = loadTags()
- })
- return tags
+ return loadTagsOnce()
}
+var loadTagsOnce = sync.OnceValue(loadTags)
+
func loadTags() map[string]bool {
tags := map[string]bool{
cfg.BuildContext.GOOS: true,
return tags
}
-var (
- anyTags map[string]bool
- anyTagsOnce sync.Once
-)
-
// AnyTags returns a special set of build tags that satisfy nearly all
// build tag expressions. Only "ignore" and malformed build tag requirements
// are considered false.
func AnyTags() map[string]bool {
- anyTagsOnce.Do(func() {
- anyTags = map[string]bool{"*": true}
- })
- return anyTags
+ return anyTagsOnce()
}
+
+var anyTagsOnce = sync.OnceValue(func() map[string]bool {
+ return map[string]bool{"*": true}
+})
counterCacheHit = counter.New("go/buildcache/hit")
counterCacheMiss = counter.New("go/buildcache/miss")
- onceIncStdlibRecompiled sync.Once
stdlibRecompiled = counter.New("go/buildcache/stdlib-recompiled")
+ stdlibRecompiledIncOnce = sync.OnceFunc(stdlibRecompiled.Inc)
)
// useCache tries to satisfy the action a, which has action ID actionHash,
counterCacheHit.Inc()
} else {
if a.Package != nil && a.Package.Standard {
- onceIncStdlibRecompiled.Do(stdlibRecompiled.Inc)
+ stdlibRecompiledIncOnce()
}
counterCacheMiss.Inc()
}