From e09f4dc6b872e29cf232511b009e69f0592e9350 Mon Sep 17 00:00:00 2001 From: Arseny Balobanov Date: Thu, 25 Feb 2021 14:42:06 +0300 Subject: [PATCH] [lectures/02-interfaces] Add errors.As example. --- lectures/02-interfaces/lecture.slide | 4 ++++ lectures/02-interfaces/temporary/main.go | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 lectures/02-interfaces/temporary/main.go diff --git a/lectures/02-interfaces/lecture.slide b/lectures/02-interfaces/lecture.slide index 5dae78b..a2006cd 100644 --- a/lectures/02-interfaces/lecture.slide +++ b/lectures/02-interfaces/lecture.slide @@ -475,3 +475,7 @@ As if perr, ok := err.(*os.PathError); ok { fmt.Println(perr.Path) } + +* errors.As + +.play temporary/main.go diff --git a/lectures/02-interfaces/temporary/main.go b/lectures/02-interfaces/temporary/main.go new file mode 100644 index 0000000..056b0f8 --- /dev/null +++ b/lectures/02-interfaces/temporary/main.go @@ -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() { + //... + } +}