Source file
doc/progs/error.go
1
2
3
4
5
6
7 package main
8
9 import (
10 "encoding/json"
11 "errors"
12 "fmt"
13 "log"
14 "net"
15 "os"
16 "time"
17 )
18
19 type File struct{}
20
21 func Open(name string) (file *File, err error) {
22
23 panic(1)
24
25 }
26
27 func openFile() {
28 f, err := os.Open("filename.ext")
29 if err != nil {
30 log.Fatal(err)
31 }
32
33
34 _ = f
35 }
36
37
38 type errorString struct {
39 s string
40 }
41
42 func (e *errorString) Error() string {
43 return e.s
44 }
45
46
47
48
49 func New(text string) error {
50 return &errorString{text}
51 }
52
53
54
55 func Sqrt(f float64) (float64, error) {
56 if f < 0 {
57 return 0, errors.New("math: square root of negative number")
58 }
59
60 return 0, nil
61 }
62
63
64
65 func printErr() (int, error) {
66 f, err := Sqrt(-1)
67 if err != nil {
68 fmt.Println(err)
69 }
70
71
72 if f < 0 {
73 return 0, fmt.Errorf("math: square root of negative number %g", f)
74 }
75
76 return 0, nil
77 }
78
79 type NegativeSqrtError float64
80
81 func (f NegativeSqrtError) Error() string {
82 return fmt.Sprintf("math: square root of negative number %g", float64(f))
83 }
84
85
86
87 type SyntaxError struct {
88 msg string
89 Offset int64
90 }
91
92 func (e *SyntaxError) Error() string { return e.msg }
93
94
95
96 func decodeError(dec *json.Decoder, val struct{}) error {
97 var f os.FileInfo
98 if err := dec.Decode(&val); err != nil {
99 if serr, ok := err.(*json.SyntaxError); ok {
100 line, col := findLine(f, serr.Offset)
101 return fmt.Errorf("%s:%d:%d: %v", f.Name(), line, col, err)
102 }
103 return err
104 }
105
106 return nil
107 }
108
109 func findLine(os.FileInfo, int64) (int, int) {
110
111 return 0, 0
112 }
113
114 func netError(err error) {
115 for {
116 if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
117 time.Sleep(1e9)
118 continue
119 }
120 if err != nil {
121 log.Fatal(err)
122 }
123
124 }
125 }
126
127 func main() {}
128
View as plain text