Chris Wiegman Chris Wiegman

Ensuring the File Path is Present to Create a File in GoLang

One thing I find myself doing often in GoLang is creating files and directories. Coming from PHP I tend to do it by writing a lot of loops or other code to get it done. With GoLang it’s quite a bit easier.

Check if the file exists:

if _, err := os.Stat("/path/to/your-file"); os.IsNotExist(err) { 
    // your file does not exist
}Code language: Go (go)

In this example we os.Stat() to get information from the file we want to create and check for if it exists by analyzing if it returned an error with os.IsNotExist(). Putting these together we now know if we need to create our file or not and, if it doesn’t, we can create it as needed.

But what if it’s folder doesn’t exist?

Of course, before we create any file, it is a good idea to make sure that path to it exists. Fortunately GoLang makes this easy as well:

os.MkdirAll("/path/to", 0700)Code language: Go (go)

Simple, right? This will recursively create the path needed. Once it’s in place we can create our file any way we want.

Putting it all together

if _, err := os.Stat("/path/to/your-file"); os.IsNotExist(err) { 
    os.MkdirAll("/path/to", 0700) // Create your file
}Code language: Go (go)

Just a bit of code is all it takes. No loops or other checking as with PHP. If only all of GoLang could be this simple.