fileutil.go 684 B

1234567891011121314151617181920212223
  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. // The third case represents the scenario where the stat returns an error,
  8. // but the error isn't relevant to the path. This can happen when the current
  9. // user doesn't have permission to access the file.
  10. func FileExists(filename string) (bool, error) {
  11. info, err := os.Stat(filename)
  12. if err != nil {
  13. if os.IsNotExist(err) {
  14. return false, nil
  15. }
  16. return false, err
  17. }
  18. return !info.IsDir(), nil
  19. }