Skip to content

Instantly share code, notes, and snippets.

@indraniel
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indraniel/310366787ea167c9b9db to your computer and use it in GitHub Desktop.
Save indraniel/310366787ea167c9b9db to your computer and use it in GitHub Desktop.
check if a file exists on the file system
// approach 1
if _, err := os.Stat("/path/to/whatever"); os.IsNotExist(err) {
// path/to/whatever does not exist
}
// approach 2
if _, err := os.Stat("/path/to/whatever"); err == nil {
// path/to/whatever exists
}
// equivalent to Python's `if os.path.exists(filename)`
if _, err := os.Stat(filename); err == nil {
fmt.Printf("file exists; processing...")
process(filename)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment