-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
135 lines (112 loc) · 2.6 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"errors"
"fmt"
"io"
"os"
)
// Custom error type
type MyError struct {
message string
}
func (e *MyError) Error() string {
return e.message
}
func main() {
// Error handling
result, err := divide(10, 0)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Result:", result)
}
// Custom error
err = customErrorFunction()
if err != nil {
fmt.Println("Custom error:", err)
}
originalErr := errors.New("database connection failed")
wrappedErr := fmt.Errorf("failed to fetch user data: %w", originalErr)
// Later in the code
fmt.Println(wrappedErr)
// Output: failed to fetch user data: database connection failed
// You can unwrap the error
unwrapped := errors.Unwrap(wrappedErr)
fmt.Println(unwrapped == originalErr) // true
// errors.Is example
if errors.Is(wrappedErr, originalErr) {
fmt.Println("wrappedErr contains originalErr")
}
// errors.As example
var myErr *MyError
err = customErrorFunction()
if errors.As(err, &myErr) {
fmt.Printf("Error is of type MyError: %v\n", myErr)
}
// Handling multiple error types
err = multipleErrorTypes()
switch {
case errors.Is(err, os.ErrNotExist):
fmt.Println("File does not exist:", err)
case errors.Is(err, os.ErrPermission):
fmt.Println("Permission denied")
case err != nil:
fmt.Println("Unknown error:", err)
}
// Panic and defer
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from panic:", r)
}
}()
panicFunction()
// This line won't be executed due to the panic
fmt.Println("This line won't be printed")
}
func divide(a, b int) (int, error) {
if b == 0 {
return 0, errors.New("division by zero")
}
return a / b, nil
}
func customErrorFunction() error {
return &MyError{message: "This is a custom error"}
}
func panicFunction() {
panic("This is a panic situation")
}
func multipleErrorTypes() error {
_, err := os.Open("non-existent-file.txt")
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
return nil
}
// Example of error wrapping with custom error types
type DatabaseError struct {
Err error
}
func (e *DatabaseError) Error() string {
return fmt.Sprintf("database error: %v", e.Err)
}
func (e *DatabaseError) Unwrap() error {
return e.Err
}
func fetchUserData() error {
err := errors.New("connection timeout")
return &DatabaseError{Err: err}
}
// Example of using io.EOF
func readUntilEOF(r io.Reader) error {
buf := make([]byte, 1024)
for {
_, err := r.Read(buf)
if err == io.EOF {
return nil // End of file, not an error
}
if err != nil {
return fmt.Errorf("read error: %w", err)
}
// Process buf...
}
}