If you have to examine if a file exists utilizing Go, then you should use one of many following strategies, relying on the model of Go it’s possible you’ll 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
}
Nevertheless, it’s also potential to entice an error from an OpenFile
try:
file, err := os.OpenFile("/path/to/file.txt")
if os.IsNotExist(err) {
// The file doesn't exists
}
You may also affirm if the trail can also be not a listing as follows:
func fileExists(filename string) bool {
information, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !information.IsDir()
}