Skip to content

Instantly share code, notes, and snippets.

@Integralist
Created January 28, 2021 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Integralist/10491f2601ffdd6bec306554470c6b0e to your computer and use it in GitHub Desktop.
Save Integralist/10491f2601ffdd6bec306554470c6b0e to your computer and use it in GitHub Desktop.
[Make Directory if it does not exist] #go #golang #directory #create #mkdir
// makeDirectoryIfNotExists asserts whether a directory exists and makes it
// if not. Returns nil if exists or successfully made.
func makeDirectoryIfNotExists(path string) error {
fi, err := os.Stat(path)
switch {
case err == nil && fi.IsDir():
return nil
case err == nil && !fi.IsDir():
return fmt.Errorf("%s already exists as a regular file", path)
case os.IsNotExist(err):
return os.MkdirAll(path, 0750)
case err != nil:
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment