-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
69 lines (60 loc) · 1.73 KB
/
main.go
File metadata and controls
69 lines (60 loc) · 1.73 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// In So it's idiomatic to communicate errors via an explicit,
// separate return value. This approach makes it easy to see which
// functions return errors and to handle them using the same language
// constructs employed for other, non-error tasks.
package main
import "solod.dev/so/errors"
// A sentinel error is a predeclared variable that
// is used to signify a specific error condition.
var ErrOutOfTea = errors.New("no more tea available")
var ErrPower = errors.New("can't boil water")
// Unlike Go, So only suppors sentinel errors.
// They must be defined at the package level using
// errors.New with a plain string message.
var Err42 = errors.New("can't work with 42")
// By convention, errors are the last return value and
// have type `error`, a built-in interface.
func f(arg int) (int, error) {
if arg == 42 {
return -1, Err42
}
// A `nil` value in the error position indicates that
// there was no error.
return arg + 3, nil
}
func makeTea(arg int) error {
if arg == 2 {
return ErrOutOfTea
} else if arg == 4 {
return ErrPower
}
return nil
}
func main() {
s := []int{7, 42}
for _, i := range s {
// It's idiomatic to use an inline
// error check in the `if` line.
if res, err := f(i); err != nil {
println("f failed:", err)
} else {
println("f worked:", res)
}
}
for i := range 5 {
if err := makeTea(i); err != nil {
// Since So only supports sentinel errors,
// they are compared with a simple pointer equality check.
// No `errors.Is` or `errors.As` needed (or supported).
if err == ErrOutOfTea {
println("We should buy new tea!")
} else if err == ErrPower {
println("Now it is dark.")
} else {
println("unknown error:", err)
}
continue
}
println("Tea is ready!")
}
}