Created
December 12, 2014 09:49
-
-
Save yujinqiu/9b5c49b60c114526ecec to your computer and use it in GitHub Desktop.
golang file exitst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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