Question: I'm trying to open a file and I'd like to know if it doesn't exist to react. But the error 

```
os.OpenFile(fName, os.O_WRONLY, 0600) 

```

returns when the file does not exist is different than os.ErrNotExists

```
os.ErrNotExists -> "file does not exist"
err.(*os.PathError).Err -> "no such file or directory"

```

os.Stat also return the same error if the file is not there. Is there a predefined error I can compare to instead of having to do it by hand?

Answer: 
> 
> [Package os](https://golang.org/pkg/os/)
> 
> 
> [func IsExist](https://golang.org/pkg/os/#IsExist)
> 
> 
> 
> ```
> func IsExist(err error) bool
> 
> ```
> 
> IsExist returns a boolean indicating whether the error is known to
>  report that a file or directory already exists. It is satisfied by
>  ErrExist as well as some syscall errors.
> 
> 
> [func IsNotExist](https://golang.org/pkg/os/#IsNotExist)
> 
> 
> 
> ```
> func IsNotExist(err error) bool
> 
> ```
> 
> IsNotExist returns a boolean indicating whether the error is known to
>  report that a file or directory does not exist. It is satisfied by
>  ErrNotExist as well as some syscall errors.
> 
> 
> 

Use the `os.IsNotExist` function. For example,

```
package main

import (
    "fmt"
    "os"
)

func main() {
    fname := "No File"
    _, err := os.OpenFile(fname, os.O_WRONLY, 0600)
    if err != nil {
        if os.IsNotExist(err) {
            fmt.Print("File Does Not Exist: ")
        }
        fmt.Println(err)
    }
}

```

Output:

```
File Does Not Exist: open No File: No such file or directory

```
Originally posted as an anonymous suggested edit to the other answer:

As of Go 1.17, the preferred way to check for this error is:

```golang
errors.Is(err, fs.ErrNotExist)

```

[Source](https://golang.google.cn/pkg/os/#IsNotExist)