Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[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
You can’t perform that action at this time.