@@ -26,52 +26,61 @@ var (
2626// initDefaultCache does the work of finding the default cache
2727// the first time Default is called.
2828func initDefaultCache () {
29- dir := os . Getenv ( "GOCACHE" )
29+ dir := DefaultDir ( )
3030 if dir == "off" {
3131 return
3232 }
33- if dir == "" {
34- // Compute default location.
35- // TODO(rsc): This code belongs somewhere else,
36- // like maybe ioutil.CacheDir or os.CacheDir.
37- switch runtime .GOOS {
38- case "windows" :
39- dir = os .Getenv ("LocalAppData" )
33+ if err := os .MkdirAll (dir , 0777 ); err != nil {
34+ base .Fatalf ("initializing cache in $GOCACHE: %s" , err )
35+ }
36+ c , err := Open (dir )
37+ if err != nil {
38+ base .Fatalf ("initializing cache in $GOCACHE: %s" , err )
39+ }
40+ defaultCache = c
41+ }
4042
41- case "darwin" :
42- dir = os .Getenv ("HOME" )
43- if dir == "" {
44- return
45- }
46- dir += "/Library/Caches"
43+ // DefaultDir returns the effective GOCACHE setting.
44+ // It returns "off" if the cache is disabled.
45+ func DefaultDir () string {
46+ dir := os .Getenv ("GOCACHE" )
47+ if dir != "" {
48+ return dir
49+ }
4750
48- case "plan9" :
49- dir = os . Getenv ( "home" )
50- if dir == "" {
51- return
52- }
53- dir += "/lib/cache"
51+ // Compute default location.
52+ // TODO(rsc): This code belongs somewhere else,
53+ // like maybe ioutil.CacheDir or os.CacheDir.
54+ switch runtime . GOOS {
55+ case "windows" :
56+ dir = os . Getenv ( "LocalAppData" )
5457
55- default : // Unix
56- // https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
57- dir = os .Getenv ("XDG_CACHE_HOME" )
58- if dir == "" {
59- dir = os .Getenv ("HOME" )
60- if dir == "" {
61- return
62- }
63- dir += "/.cache"
64- }
58+ case "darwin" :
59+ dir = os .Getenv ("HOME" )
60+ if dir == "" {
61+ return "off"
6562 }
66- dir = filepath .Join (dir , "go-build" )
67- if err := os .MkdirAll (dir , 0777 ); err != nil {
68- return
63+ dir += "/Library/Caches"
64+
65+ case "plan9" :
66+ dir = os .Getenv ("home" )
67+ if dir == "" {
68+ return "off"
6969 }
70- }
70+ // Plan 9 has no established per-user cache directory,
71+ // but $home/lib/xyz is the usual equivalent of $HOME/.xyz on Unix.
72+ dir += "/lib/cache"
7173
72- c , err := Open (dir )
73- if err != nil {
74- base .Fatalf ("initializing cache in $GOCACHE: %s" , err )
74+ default : // Unix
75+ // https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
76+ dir = os .Getenv ("XDG_CACHE_HOME" )
77+ if dir == "" {
78+ dir = os .Getenv ("HOME" )
79+ if dir == "" {
80+ return "off"
81+ }
82+ dir += "/.cache"
83+ }
7584 }
76- defaultCache = c
85+ return filepath . Join ( dir , "go-build" )
7786}
0 commit comments