[lectures/02-interfaces] Add errors.As example.
This commit is contained in:
parent
caff45e18c
commit
e09f4dc6b8
2 changed files with 22 additions and 0 deletions
|
@ -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
|
||||||
|
|
18
lectures/02-interfaces/temporary/main.go
Normal file
18
lectures/02-interfaces/temporary/main.go
Normal 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() {
|
||||||
|
//...
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue