Skip to content

Change !os.IsNotExist to os.IsExist#787

Closed
Smarticles101 wants to merge 1 commit intoexercism:masterfrom
Smarticles101:development
Closed

Change !os.IsNotExist to os.IsExist#787
Smarticles101 wants to merge 1 commit intoexercism:masterfrom
Smarticles101:development

Conversation

@Smarticles101
Copy link
Member

No description provided.

@Smarticles101
Copy link
Member Author

Okay... So os.IsExist(err) != !os.IsNotExist(err). I thought the contrary but looking into it, I found this gist which explains it pretty well. Basically, the methods are just error checkers, they check for errors that are expected if a file exists or doesn't.

Since we query the file with Lstat, we expect the file to exist already, so it wouldn't throw an error if the file exists.

My fault for not testing it before I pushed it, but cool to learn that.


// If it already exists don't clobber it with the default.
if _, err := os.Lstat(workspace); !os.IsNotExist(err) {
if _, err := os.Lstat(workspace); os.IsExist(err) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.Lstat will return nil if the path exist. os.IsExist requires error to be ErrExist which is why file existence is being checked with !os.IsNotExist; Lstat will always return ErrNotExist if the path is not found otherwise nil (if there are no other errors).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I found that out lol. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants