-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo_error.go
69 lines (60 loc) · 1.44 KB
/
go_error.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* Module: go_error.go
* Purpose: GO Error processing test
* Date: N/A
* Notes:
* 1) To build:
* go build go_error.go
* 2) Ref: https://tour.golang.org/methods/8
*
*/
package main
import (
"fmt"
"time"
"strconv"
)
// Error structure with time and a message string
type MyError struct {
When time.Time
What string
}
// Implement the Error() function, implementing the interface
// format the string with the time and message
// type error interface {
// Error() string
// }
//
func (e *MyError) Error() string {
return fmt.Sprintf("ERROR: %v, %s", e.When, e.What)
}
// Dummy function called run() to return an error w/ time and msg
func run() error {
return &MyError{
time.Now(),
"run() failed",
}
}
// Main function, calls the run() and checks for errors
func main() {
fmt.Println("go_error.go: Error examples")
fmt.Println("Test dummy function returning error (should report error)")
// precursor ; if test -- if with initialization
if err := run(); err != nil {
fmt.Println(err)
}
istr:="42"
fmt.Println("Trying to convert", istr, " (should be OK)")
i, err := strconv.Atoi(istr)
if err != nil {
fmt.Printf("couldn't convert number: %v\n", err)
}
fmt.Println("Converted integer:", i)
istr="xx"
fmt.Println("Trying to convert", istr, " (should fail)")
i, err = strconv.Atoi(istr)
if err != nil {
fmt.Printf("couldn't convert number: %v\n", err)
}
fmt.Println("Converted integer:", i)
}