Puzzle 19 Explanation: Go Error
Understand how err is used in Go.
We'll cover the following...
We'll cover the following...
Try it yourself
Try executing the code below to see the result.
Go (1.16.5)
package mainimport ("fmt")type OSError intfunc (e *OSError) Error() string {return fmt.Sprintf("error #%d", *e)}func FileExists(path string) (bool, error) {var err *OSErrorreturn false, err}func main() {if _, err := FileExists("/no/such/file"); err != nil {fmt.Printf("error: %s\n", err)} else {fmt.Println("OK")}}
Explanation
The if statement in line 19 says that err != nil
, but err
prints out as < nil >
.
Furthermore, err
in line 14 ...