fileutil.go 684 B

123456789101112131415161718192021222324
  1. package fileutil
  2. import "os"
  3. // File exists has three different return cases that should be handled:
  4. // 1. File exists and is not a directory (true, nil)
  5. // 2. File does not exist (false, nil)
  6. // 3. File may or may not exist. Error occurred during stat (false, error)
  7. //
  8. // The third case represents the scenario where the stat returns an error,
  9. // but the error isn't relevant to the path. This can happen when the current
  10. // user doesn't have permission to access the file.
  11. func FileExists(filename string) (bool, error) {
  12. info, err := os.Stat(filename)
  13. if err != nil {
  14. if os.IsNotExist(err) {
  15. return false, nil
  16. }
  17. return false, err
  18. }
  19. return !info.IsDir(), nil
  20. }