Go functions to create a directory if it doesn't exist
Create a directory if it doesn't exist.
  
  Don't report an error if the directory exists.




Isn't that a simple wrapper for the Go standard library os.Mkdir function?

For example,

// mkdir, like os.Mkdir, creates a new directory
// with the specified name and permission bits.
// If the directory exists, mkdir returns nil.
func mkdir(name string, perm os.FileMode) error {
    err := os.Mkdir(name, perm)
    if err != nil {
        if !os.IsExist(err) {
            return err
        }
    }
    return nil
}





  Personally I prefer the one line method.


In Go, we favor readability and simplicity for correct and maintainable code. Use the Go idiom for creating a file:

f, err := Create(fileName)
if err != nil {
    // handle error
}
defer f.Close()


For your usage, use a wrapper for the Go standard library os.Create function.

For example,

// createFT, like os.Create, creates the named file with mode 0666 (before umask),
// truncating it if it already exists.
// The file is created in the directory named in the FT_SOURCE environment variable.
func createFT(fileName string) (*os.File, error) {
    dirKey := "FT_SOURCE"
    dirName, ok := os.LookupEnv(dirKey)
    if !ok {
        err := fmt.Errorf("%s environmemt variable not found", dirKey)
        return nil, err
    }

    perm := os.FileMode(0666)
    err := mkdir(dirName, perm)
    if err != nil {
        return nil, err
    }

    f, err := os.Create(filepath.Join(dirName, fileName))
    if err != nil {
        return nil, err
    }
    return f, nil
}


Functions are used to encapsulate implementation details. Functions can also be used to hide complexity and "ugly" code.