os.Create returns an error as the second return value. The error itself is an interface type error interface { Error() string } . Any data type that has the Error method implements this interface and can be assigned.
In most cases, just printing an error is enough, but in this example you want to explicitly handle ENOSPC (without a space on the device). The os package returns *os.PathError as the implementation of the error in this case, and if you want to get additional information about the error, i.e. Anything next to the Error() string method, you will have to convert it.
The statement e, ok := err.(*os.PathError) is a type statement. It will check if the err *os.PathError interface value is as a specific type and return it. If another type was stored in the interface (there may be other types that implement the Error interface), then it will simply return zero and false, i.e. nil, false in this case.
source share