...

/

Puzzle 19 Explanation: Go Error

Puzzle 19 Explanation: Go Error

Understand how err is used in Go.

We'll cover the following...

Try it yourself

Try executing the code below to see the result.

Press + to interact
package main
import (
"fmt"
)
type OSError int
func (e *OSError) Error() string {
return fmt.Sprintf("error #%d", *e)
}
func FileExists(path string) (bool, error) {
var err *OSError
return 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 ...

Access this course and 1400+ top-rated courses and projects.