Last active
November 6, 2021 19:58
-
-
Save miguelmota/9ab72c5e342f833123c0b5cfd5aca468 to your computer and use it in GitHub Desktop.
Golang expand tilde home directory
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
if strings.HasPrefix(path, "~/") { | |
path = filepath.Join(dir, path[2:]) | |
} |
Since go 1.12
if strings.HasPrefix(path, "~/") {
dirname, _ := os.UserHomeDir()
path = filepath.Join(dirname, path[2:])
}
Found on https://stackoverflow.com/a/13004756
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try https://github.com/mitchellh/go-homedir. It's Mitchell Hashimoto's code, so it'll be good ;-)