Junior — Senior
Comparison behavior of error with nil when returning a null pointer
livecode
Task condition
It is necessary to investigate how a function that returns an error type works and what strings will appear in the output when checking the result for nil. Special attention should be paid to the fact that the error interface can hold a nil pointer of a specific type (for example, *os.PathError), which changes the comparison result with nil.
func someFunc(shouldReturnErr bool) error {
var err *os.PathError = nil
if shouldReturnErr {
return errors.New("time to throw error")
}
return err
}
func main() {
errTrue := someFunc(true)
fmt.Println(errTrue)
fmt.Println(errTrue == nil)
fmt.Println()
errFalse := someFunc(false)
fmt.Println(errFalse)
fmt.Println(errFalse == nil)
}
Analyze what values the program will output in both cases and why the comparison with nil gives an expected or unexpected result.