* DrGo <salah.mah...@gmail.com> [180806 02:00]:
> Thanks,
> filepath.Abs does some of what I want, but as far as I could tell, it does 
> not handle resolving relative paths

Does this code help out?

if filepath.IsAbs(somepath) {
    somepath = filepath.Clean(somepath)
} else {
    var wd, err = os.Getwd()
    if err != nil {
        // Handle error
    }
    somepath = filepath.Join(wd, somepath)
}

It isn't much more code to handle an optionally provided default
directory in place of the current working directory.

You mentioned checking if a file must or must not exist.  Usually, doing
so before attempting to actually open or create the file is at best
redundant and at worst the wrong thing to do.  The correct way to do
this is almost always to use os.OpenFile with the appropriate flag (e.g.
os.O_CREATE and/or os.O_EXCL), and handle the error.

The reason is that between checking for existence and actually opening
or creating the file, the file may be created or deleted.  OpenFile will
give you an error if your desired condition fails and an open file if it
succeeds, and it will be atomic (at least to the best of the
capabilities of your OS/filesystem combination).

...Marvin

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to