1

e.g.

a/b/c* -> a/b
a/b/c*/*/*b -> a/b

Why I need this is because I want to get the abs path of globed filename. Code example:

files, _ := filepath.Glob(p)
top := __magic here__
for _, f := range files {
    abs, _ := filepath.Abs(path.Join(top, f))
    fmt.Println(abs)
}

Is there any exists method for this purpose? otherwise I have to implement by myself.

EDIT

The magic is make glob path abs first, then the glob return abs path.

wener
  • 7,191
  • 6
  • 54
  • 78

1 Answers1

0

File names returned by filepath.Glob() are already absolute (but read below).

See this example:

fs, err := filepath.Glob("/dev/../dev/*")
if err != nil {
    panic(err)
}
for _, f := range fs {
    fmt.Println(f, filepath.IsAbs(f))
}

Output:

/dev/null true
/dev/random true
/dev/urandom true
/dev/zero true

Try it on the Go Playground.

Edit:

The returned file names are only absolute if the glob pattern is absolute. So the easiest way is to make the glob pattern absolute.

icza
  • 389,944
  • 63
  • 907
  • 827
  • 1
    If you use the relative path to glob, the path is relative, try [this](http://play.golang.org/p/XiOivBFEhE), but thank you, I can make glob path abs then do glob, the path will be abs. – wener Jun 26 '15 at 12:00