[lectures/02-interfaces] Add errors.As example.

This commit is contained in:
Arseny Balobanov 2021-02-25 14:42:06 +03:00
parent caff45e18c
commit e09f4dc6b8
2 changed files with 22 additions and 0 deletions

View file

@ -475,3 +475,7 @@ As
if perr, ok := err.(*os.PathError); ok { if perr, ok := err.(*os.PathError); ok {
fmt.Println(perr.Path) fmt.Println(perr.Path)
} }
* errors.As
.play temporary/main.go

View file

@ -0,0 +1,18 @@
package main
import "errors"
type Temporary interface {
IsTemporary() bool
}
func do() error { return nil }
func main() {
err := do()
var terr Temporary
if errors.As(err, &terr) && terr.IsTemporary() {
//...
}
}