V
V
Vadim Rublev2020-01-18 07:31:40
go
Vadim Rublev, 2020-01-18 07:31:40

The essence of the IsNotExist() function?

What is the meaning of the os.IsNotExist() function? She's using the os.Stat() function, getting her error - if _, err := os.Stat(filename); os.IsNotExist(err) { ... }.
( Info: https://golang.org/pkg/os/#IsNotExist )
Why not just use the os.Stat() function? (As some people do.)
And along the way: IsNotExist() function returns true - if the file does not exist, and false - if the file exists? i.e .?:
if _, err := os.Stat(filename); os.IsNotExist(err) {
fmt.Println("file does not exist") is_true
} else {
_file_found_ is_false
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav, 2020-01-18
@Vadim Rublev

os.IsNotExist(err) checks for an error that it is a file missing error, you can check for an error not only Stat, but also an error when opening a file.
Stat can return not only a file not found error, but also an I / O error, a timeout and a bunch of errors, for this an additional error check os.IsNotExist(err) is used
Wrong conclusion about the code example, right like this:

_, err := os.Stat(filename)
if err != nil {
    if os.IsNotExist(err) {
        fmt.Println("file does not exist") // это_true
    } else {
    // другая ошибка  - это_false
    }
} else {
    // тут файл существует 
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question