Source: from https://golangr.com/file-exists/
I didn't just learn this, but for some reason I always have to look this idiom up.
The following Go code will check if the specified file exists or not:
package main
import "fmt"
import "os"
func main() {
if _, err := os.Stat("file-exists.go"); err == nil {
fmt.Printf("File exists\n");
} else {
fmt.Printf("File does not exist\n");
}
}
If you want to check if a file exists before continuing the program:
package main
import "fmt"
import "os"
func main() {
if _, err := os.Stat("file-exists2.file"); os.IsNotExist(err) {
fmt.Printf("File does not exist\n");
}
// continue program
}