If it is advisable test if a file exists utilizing Go, then you need to use one of many following strategies, relying on the model of Go you could be utilizing.
In case you are utilizing Go >=1.13, then
file, err := os.OpenFile("/path/to/file.txt")
if errors.Is(err, os.ErrNotExist) {
// The file doesn't exists
}
Nonetheless, it’s also doable to lure an error from an OpenFile
try:
file, err := os.OpenFile("/path/to/file.txt")
if os.IsNotExist(err) {
// The file doesn't exists
}
You can too affirm if the trail can be not a listing as follows:
func fileExists(filename string) bool {
data, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !data.IsDir()
}