Hopefully I'm not missing the obvious here, but I don't see a function anywhere to check if a file exists.
In so far as I can tell, os.Stat does me no good for a file that does not exist:path := "/does/not/exist"fi, err := os.Stat(path)if fi == nil && err != nil {fmt.Printf("%v\n", err) //"stat /does/not/exist: no such file or directory"}So is fi nil because it doesn't exist, or some other reason? I can't tell. The error message tells me it doesn't exist, so am I to parse the error output to detect if that is the case? There's got to be a better way. If not, why does the documentation mention os.ENOENT? Thanks.
> if err == os.ENOENT {
> fmt.Printf("%s not found\n", path)
> } else if err != nil {
> fmt.Printf("%s, %v\n", path, err)
> }
This in particular boils down to
fmt.Printf("%s\n", err)
In either case it will already say the name and what went wrong.
Usually it is better to try the operation you care about and
let the OS tell you whether it worked, instead of trying to predict
whether it will work beforehand.
Russ
It's a type assertion. If err is of actual type *os.PathError, assign the
value to (a probably freshly declared) e (which will/should have type
*os.PathError) and true to ok; otherwise assign false to ok and
a zero value to e.
Note that os.Error is an interface type.
Chris
--
Chris "allusive" Dollin