2
0

fileutil.go 689 B

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