A method of the go language to detect the existence of a file

  • 2020-05-26 09:20:26
  • OfStack

This article demonstrates an example of how the go language detects the existence of a file. Share with you for your reference. The specific analysis is as follows:

The go language detects the existence of a file by first creating an FileInfo and, if no error is reported, checking if it is a directory through IsDir()

finfo, err := os.Stat("filename.txt")
if err != nil {
    // no such file or dir
    return
}
if finfo.IsDir() {
    // it's a file
} else {
    // it's a directory
}

I hope this article has helped you with the programming of the Go language.


Related articles: