Using
os.Stat() and os.IsNotExist() functions, you can check whether a file exists or
not.
func
isFileExists(filePath string) bool {
_, err := os.Stat(filePath)
return !os.IsNotExist(err)
}
Above
function return true, if the files exists, else false.
App.go
package main import ( "fmt" "os" ) func isFileExists(filePath string) bool { _, err := os.Stat(filePath) return !os.IsNotExist(err) } func main() { existingFile := "/Users/harikrishnagurram/Downloads/Welcome.pdf" nonExistingFile := "/Users/harikrishnagurram/Downloads/FileNotExists.pdf" fmt.Println(isFileExists(existingFile)) fmt.Println(isFileExists(nonExistingFile)) }
Output
true
false
No comments:
Post a Comment