-
Notifications
You must be signed in to change notification settings - Fork 0
/
errorhandling.go
53 lines (42 loc) · 899 Bytes
/
errorhandling.go
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
/*
Errors
By default, errors are the last return value
and have type `error`, a built-in interface
errors.New constructs a basic `error` value with
the given error message
A `nil` value in the error position indicates that
there was no error
It's possible to use custom types as `errors` by
implementing Error() method on them.
*/
package main
import (
"errors"
"fmt"
)
func main() {
fmt.Println(err2(42))
}
func err1(arg int) (int, error) {
if arg == 42 {
return arg, errors.New("can't work with 42")
}
return arg + 10, nil
}
/*
Custom types implemented as error by implementing
Error() method on them.
*/
type argumentError struct {
arg int
prob string
}
func (e *argumentError) Error() string {
return fmt.Sprintf("%d - %s", e.arg, e.prob)
}
func err2(arg int) (int, error) {
if arg == 42 {
return -1, &argumentError{arg, "can't work with 42"}
}
return arg + 3, nil
}