The Go language is used to determine whether a file or folder exists
- 2020-06-03 06:56:19
- OfStack
This article gives an example of how the Go language determines whether a file or folder exists. To share for your reference, specific as follows:
Golang determines if a file is a bit weird based on the error message it returns while manipulating the file, not directly based on the path
Version 1:
func IsExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
Version 2: Lite version
func IsExist(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
// or
//return err == nil || !os.IsNotExist(err)
// or
//return !os.IsNotExist(err)
}
I hope this article has been helpful in Go programming.