Go: Check if a file or directory exists

Use the os.Stat and os.IsNotExist functions:

filepath := "/dev/null"
if _, err := os.Stat(filepath); !os.IsNotExist(err) {
	fmt.Println(filepath, "exists")
	// If there was an error, it could for instance
	// be related to a permission error.


/dev/null exists
Warning: A file that is hidden from the current process (or user executing the process) will, of course, not be visible.


Be the first to comment!