What is err.(*os.PathError) in Go?
go
Solution
`os.Create` returns an error as second return value. The error itself is an interface `type error interface { Error() string }`. Any data type that happens to have a `Error` method will implement that interface and can be assigned.
In most cases, just printing the error is enough, but in this example, you would like to handle `ENOSPC` (no space left on device) explicitly. The `os` package returns an `*os.PathError` as error implementation in that case and if you want to access additional information about the error, i.e. everything beside the `Error() string`, method, you would have to convert it.
The statement `e, ok := err.(*os.PathError)` is a type assertion. It will check if the interface value `err` contains a `*os.PathError` as concrete type and will return that. If another type was stored in the interface (there might be other types that implement the `error` interface) then it will simply return the zero value and false, i.e. `nil, false` in that case.
Problem
When I was reading: http://golang.org/doc/effective_go.html#errors I found such line: `err.(*os.PathError)` in this context: ``` 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 } ``` What exactly is `err.(*os.PathError)` in Go?