Skip to content

Commit fa1a49a

Browse files
committed
os: add UserHomeDir
Fixes #26463 Change-Id: Iaef1c7456ffaeadeead6027a37d09c44a3d05bd5 Reviewed-on: https://go-review.googlesource.com/c/139418 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 86e251c commit fa1a49a

File tree

3 files changed

+51
-14
lines changed

3 files changed

+51
-14
lines changed

src/os/example_test.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,29 @@ func ExampleIsNotExist() {
7676
// file does not exist
7777
}
7878

79-
func init() {
80-
os.Setenv("USER", "gopher")
81-
os.Setenv("HOME", "/usr/gopher")
82-
os.Unsetenv("GOPATH")
83-
}
84-
8579
func ExampleExpand() {
8680
mapper := func(placeholderName string) string {
8781
switch placeholderName {
8882
case "DAY_PART":
8983
return "morning"
90-
case "USER":
84+
case "NAME":
9185
return "Gopher"
9286
}
9387

9488
return ""
9589
}
9690

97-
fmt.Println(os.Expand("Good ${DAY_PART}, $USER!", mapper))
91+
fmt.Println(os.Expand("Good ${DAY_PART}, $NAME!", mapper))
9892

9993
// Output:
10094
// Good morning, Gopher!
10195
}
10296

10397
func ExampleExpandEnv() {
104-
fmt.Println(os.ExpandEnv("$USER lives in ${HOME}."))
98+
os.Setenv("NAME", "gopher")
99+
os.Setenv("BURROW", "/usr/gopher")
100+
101+
fmt.Println(os.ExpandEnv("$NAME lives in ${BURROW}."))
105102

106103
// Output:
107104
// gopher lives in /usr/gopher.
@@ -117,16 +114,24 @@ func ExampleLookupEnv() {
117114
}
118115
}
119116

120-
show("USER")
121-
show("GOPATH")
117+
os.Setenv("SOME_KEY", "value")
118+
os.Setenv("EMPTY_KEY", "")
119+
120+
show("SOME_KEY")
121+
show("EMPTY_KEY")
122+
show("MISSING_KEY")
122123

123124
// Output:
124-
// USER=gopher
125-
// GOPATH not set
125+
// SOME_KEY=value
126+
// EMPTY_KEY=
127+
// MISSING_KEY not set
126128
}
127129

128130
func ExampleGetenv() {
129-
fmt.Printf("%s lives in %s.\n", os.Getenv("USER"), os.Getenv("HOME"))
131+
os.Setenv("NAME", "gopher")
132+
os.Setenv("BURROW", "/usr/gopher")
133+
134+
fmt.Printf("%s lives in %s.\n", os.Getenv("NAME"), os.Getenv("BURROW"))
130135

131136
// Output:
132137
// gopher lives in /usr/gopher.

src/os/file.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,24 @@ func UserCacheDir() (string, error) {
381381
return dir, nil
382382
}
383383

384+
// UserHomeDir returns the current user's home directory.
385+
//
386+
// On Unix, including macOS, it returns the $HOME environment variable.
387+
// On Windows, it returns the concatenation of %HOMEDRIVE% and %HOMEPATH%.
388+
// On Plan 9, it returns the $home environment variable.
389+
func UserHomeDir() string {
390+
if runtime.GOOS == "windows" {
391+
return Getenv("HOMEDRIVE") + Getenv("HOMEPATH")
392+
}
393+
if runtime.GOOS == "plan9" {
394+
return Getenv("home")
395+
}
396+
if runtime.GOOS == "nacl" {
397+
return "/"
398+
}
399+
return Getenv("HOME")
400+
}
401+
384402
// Chmod changes the mode of the named file to mode.
385403
// If the file is a symbolic link, it changes the mode of the link's target.
386404
// If there is an error, it will be of type *PathError.

src/os/os_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,3 +2333,17 @@ func TestDoubleCloseError(t *testing.T) {
23332333
t.Logf("second close returned expected error %q", err)
23342334
}
23352335
}
2336+
2337+
func TestUserHomeDir(t *testing.T) {
2338+
dir := UserHomeDir()
2339+
if dir == "" {
2340+
t.Fatal("UserHomeDir returned an empty string")
2341+
}
2342+
fi, err := Stat(dir)
2343+
if err != nil {
2344+
t.Fatal(err)
2345+
}
2346+
if !fi.IsDir() {
2347+
t.Fatalf("dir %s is not directory; type = %v", dir, fi.Mode())
2348+
}
2349+
}

0 commit comments

Comments
 (0)