Skip to content

Instantly share code, notes, and snippets.

@yujinqiu
Created December 12, 2014 09:49
Show Gist options
  • Save yujinqiu/9b5c49b60c114526ecec to your computer and use it in GitHub Desktop.
Save yujinqiu/9b5c49b60c114526ecec to your computer and use it in GitHub Desktop.
golang file exitst
// equivalent to Python's `if not os.path.exists(filename)`
if _, err := os.Stat(filename); os.IsNotExist(err) {
fmt.Printf("no such file or directory: %s", filename)
return
}
In the above example we are not checking if err != nil because os.IsNotExist(nil) == false anyway.
To check if a file 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