Skip to content

Instantly share code, notes, and snippets.

@pasela
Created October 28, 2014 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pasela/0450d041abd6723facb1 to your computer and use it in GitHub Desktop.
Save pasela/0450d041abd6723facb1 to your computer and use it in GitHub Desktop.
[go] ExpandTilde
import (
"os/user"
"strings"
)
func ExpandTilde(path string) (string, error) {
if path[0] != '~' {
return path, nil
}
si := strings.IndexAny(path, "/\\")
var head, tail string
if si == -1 {
head, tail = path, ""
} else {
head, tail = path[:si], path[si:]
}
var usr *user.User
var err error
if head == "~" {
usr, err = user.Current()
} else {
usr, err = user.Lookup(head[1:])
}
if err != nil {
return path, err
}
return usr.HomeDir + tail, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment