检查来自os.Remove的错误消息

What's the most idiomatic way to check error messages? My use case is that in err := os.Remove(path), I consider a success either:

A) if err == nil

or

B) if err != nil but the error is thrown because the file was not found.

any other error should cause the removal to retry. Currently I've wrapped this in a for { ... } loop and am checking:

if err == nil || strings.Contains(err.Error(), "no such file") {
    // Success
} else {
    // Fail
}

Since the docs say:

If there is an error, it will be of type *PathError.

I don't think there's a way to check by type assertion. Am I missing something fundamental? My error handling in Go has always felt somewhat slapdash.

The "type" error is an interface. Interfaces don't have an concrete type. To get the type of the value you could use a type assertion or a type switch:

// Type assertion
_, ok := err.(*PathError)

// Type switch
switch err.(type) {
case *PathError:
    // You know the type now
}

This is an idiomatic way to find out of which type an error is. As in the comments specified there is already a function inside the os package which does this for you (https://golang.org/pkg/os/#IsNotExist)

I just dealt with this the other day. An error from os.Remove() will be syscall.ENOENT if the file did not exist.

So you can use logic like this:

if err != nil {
    e, ok := err.(*os.PathError)
    if ok && e.Err == syscall.ENOENT {
        // The file didn't exist
        w.WriteHeader(http.StatusNotFound)
        return
    } else {
        // Error other than syscall.ENOENT
    }
}

Of course, as shown in another answer, os.IsNotExist() is simple and idiomatic. Wish I'd seen that before today.