Check If a File Exists Before Using It
·
184 words
·
1 minute read
In this basic example, we check to see if a file exists before interacting with it (otherwise something’s not going to work as expected). We leverage the power of the os
standard library and first use the Stat()
function, which although it’s usually used to get information about a file, we’re only looking at the errors.
We can’t just check for err == nil
because any number of errors could be returned so we pass it to IsNotExists()
to confirm that it’s an error because the file does not exist.