forked from zhravan/golearn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
38 lines (32 loc) · 857 Bytes
/
Copy patherrors.go
File metadata and controls
38 lines (32 loc) · 857 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package errors
import (
"fmt"
"os"
)
// divZeroErr is a minimal error type whose message is "division by zero".
type divZeroErr struct{}
// Error makes divZeroErr implement error.
func (divZeroErr) Error() string { return "division by zero" }
// Is allows errors.Is(err, errors.New("division by zero")) to succeed.
func (divZeroErr) Is(target error) bool {
if target == nil {
return false
}
return target.Error() == "division by zero"
}
// divide returns a/b or a "division by zero" error when b == 0.
func divide(a, b int) (int, error) {
if b == 0 {
return 0, divZeroErr{}
}
return a / b, nil
}
// processDivision prints the result or the error to stdout.
func processDivision(a, b int) {
res, err := divide(a, b)
if err != nil {
fmt.Fprint(os.Stdout, "Error: ", err, "\n")
return
}
fmt.Fprint(os.Stdout, "Result: ", res, "\n")
}