-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
46 lines (39 loc) · 1.02 KB
/
main.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
package errors
import "fmt"
/*
In golang it is perfectly normal for functions & methods to return an error to indicate an exception
which differs greatly from most languages. By convention the error should be the last item returned.
*/
func Run() {
_, _ = returningAnError(15)
handlingAnError()
recoveringIfPanics()
}
type MyNewError struct {
number int
}
func (m *MyNewError) Error() string {
return fmt.Sprintf("%d was less than 10!", m.number)
}
// An example of conditionally returning an error under some circumstance.
func returningAnError(argument int) (int, error) {
if argument < 10 {
return argument, &MyNewError{number: argument}
}
return argument, nil
}
// How to handle an error, 'raising' if something bad happens
func handlingAnError() {
_, err := returningAnError(100)
if err != nil {
panic(err)
}
}
// Panics can be 'caught' and handled using the `recover` builtin idiom
func recoveringIfPanics() {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from error:\n", r)
}
}()
}