Go\'s standard library does not have a function solely intended to check if a file exists or not (like Python\'s os.path.exists). What is the idiomatic way
Answer by Caleb Spare posted in gonuts mailing list.
[...] It's not actually needed very often and [...] using
os.Statis easy enough for the cases where it is required.[...] For instance: if you are going to open the file, there's no reason to check whether it exists first. The file could disappear in between checking and opening, and anyway you'll need to check the
os.Openerror regardless. So you simply callos.IsNotExist(err)after you try to open the file, and deal with its non-existence there (if that requires special handling).[...] You don't need to check for the paths existing at all (and you shouldn't).
os.MkdirAllworks whether or not the paths already exist. (Also you need to check the error from that call.)Instead of using
os.Create, you should useos.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666). That way you'll get an error if the file already exists. Also this doesn't have a race condition with something else making the file, unlike your version which checks for existence beforehand.
Taken from: https://groups.google.com/forum/#!msg/golang-nuts/Ayx-BMNdMFo/4rL8FFHr8v4J