Created
October 8, 2020 12:01
-
-
Save mgdelacroix/58542dcd45d8712751780d58b7a70477 to your computer and use it in GitHub Desktop.
Mock user directory in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"os" | |
"os/user" | |
) | |
func main() { | |
dir, _ := ioutil.TempDir("", "mmctl-") | |
defer os.RemoveAll(dir) | |
u, err := user.Current() | |
if err != nil { | |
panic(err) | |
} | |
oldHomeDir := u.HomeDir | |
u.HomeDir = dir | |
defer func() { | |
u.HomeDir = oldHomeDir | |
}() | |
fmt.Printf("User home is: %s\n", u.HomeDir) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment