Skip to content

Instantly share code, notes, and snippets.

@mgdelacroix
Created October 8, 2020 12:01
Show Gist options
  • Save mgdelacroix/58542dcd45d8712751780d58b7a70477 to your computer and use it in GitHub Desktop.
Save mgdelacroix/58542dcd45d8712751780d58b7a70477 to your computer and use it in GitHub Desktop.
Mock user directory in Go
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