go languageTo detect whether the file exists, first create a FileInfo. If no error is reported, then check whether it is a directory through IsDir()
Code As follows:
finfo, err := os.Stat("filename.js") if err != nil { // no such file or dir return } if finfo.IsDir() { // it's a file } else { // it's a directory }
The method for golang to determine whether a file or folder exists is to use the error value returned by the os.Stat() function to determine:
1. If the returned error is nil, explain The file or folder exists
2. If the error type returned is true using os.IsNotExist(), it means the file or folder does not exist
3. If the error type returned is other types , then you are not sure whether it exists.
For more golang knowledge, please pay attention to thegolang tutorialcolumn on the PHP Chinese website.
The above is the detailed content of Golang determines whether js file exists. For more information, please follow other related articles on the PHP Chinese website!