Go: error 'type assertion' vs 'errors.Is()' vs '=='

Example from effective go:

for try := 0; try < 2; try++ {
    file, err = os.Create(filename)
    if err == nil {
        return
    }
    if e, ok := err.(*os.PathError); ok && e.Err == syscall.ENOSPC {
        deleteTempFiles()  // Recover some space.
        continue
    }
    return
}

if e, ok := err.(*os.PathError);

"type assertion" vs "errors.Is()"

e.Err implements the error interface; syscall.ENOSPC is a constant. How can you compare an error to a constant?

"errors.Is()" vs "==" (equality operator)

Last updated