Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active November 6, 2021 19:58
Show Gist options
  • Save miguelmota/9ab72c5e342f833123c0b5cfd5aca468 to your computer and use it in GitHub Desktop.
Save miguelmota/9ab72c5e342f833123c0b5cfd5aca468 to your computer and use it in GitHub Desktop.
Golang expand tilde home directory
if strings.HasPrefix(path, "~/") {
path = filepath.Join(dir, path[2:])
}
@soniah
Copy link

soniah commented Jul 17, 2018

Try https://github.com/mitchellh/go-homedir. It's Mitchell Hashimoto's code, so it'll be good ;-)

@sebble
Copy link

sebble commented Nov 6, 2021

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