Created
January 28, 2021 10:29
-
-
Save Integralist/10491f2601ffdd6bec306554470c6b0e to your computer and use it in GitHub Desktop.
[Make Directory if it does not exist] #go #golang #directory #create #mkdir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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