Dealing with files in Go


File existence

Useful when you want to have consistency checking file existence, with a bit more granularity.

func exists(file string) (bool, error) {
    _, err := os.Stat(file)
    // stat success, file exists, exit early
    if err == nil {
        return true, nil
    }
    // check for an os.ErrNotExist, "file does not exist". Return false and no errors.
    if os.IsNotExist(err) {
        return false, nil
    }
    // error from stat
    return false, err
}

Used as follows:

if ok, err := exists(f); err != nil {
    return err // this is a true error
} else if !ok {
    // file does not exist
} 
// file exists...

Creating files

This will truncate an existing file, no questions asked.

f, err := os.Create(filename)
if err != nil {
    // error
}
if err = f.Close(); err != nil {
    // error
}

A slightly better approach is to use os.OpenFile, because it enables tuning how the file is opened through flags. Append only, fail hard if file exists, etc.

// open write-only file, create if non-existent, append data when writing
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)

// open read-write file, create if non-existent only, error if exists
// can check for os.ErrExist err by calling os.IsExist()
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0644)

// open read-write file, create if non-existent, truncate if exists (similar behavior to os.Create())
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)

if err != nil {
    // error
}
if err := f.Close(){
    // error
}