-
Notifications
You must be signed in to change notification settings - Fork 168
/
error_4.go
113 lines (97 loc) · 3.17 KB
/
error_4.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
// -------------------
// Behavior as context
// -------------------
// Behavior as context allows us to use a custom error type as our context but avoid that type
// assertion back to the concrete. We get to maintain a level of decoupling in our code.
package main
import (
"bufio"
"fmt"
"io"
"log"
"net"
)
// client represents a single connection in the room.
type client struct {
name string
reader *bufio.Reader
}
// TypeAsContext shows how to check multiple types of possible custom error
// types that can be returned from the net package.
func (c *client) TypeAsContext() {
for {
// We are using reader interface value to decouple ourselves from the network read.
line, err := c.reader.ReadString('\n')
if err != nil {
// This is using type as context like the previous example.
// What special here is the method named Temporary. If it is, we can keep going but if not,
// we have to break thing down and build thing back up.
// Every one of these cases care only about 1 thing: the behavior of Temporary. This is
// what important. We can switch here, from type as context to type as behavior if we
// do this type assertion and only ask about the potential behavior of that concrete
// type itself.
// We can go ahead and declare our own interface called temporary like below.
switch e := err.(type) {
case *net.OpError:
if !e.Temporary() {
log.Println("Temporary: Client leaving chat")
return
}
case *net.AddrError:
if !e.Temporary() {
log.Println("Temporary: Client leaving chat")
return
}
case *net.DNSConfigError:
if !e.Temporary() {
log.Println("Temporary: Client leaving chat")
return
}
default:
if err == io.EOF {
log.Println("EOF: Client leaving chat")
return
}
log.Println("read-routine", err)
}
}
fmt.Println(line)
}
}
// temporary is declared to test for the existence of the method coming from the net package.
// Because Temporary is the only behavior we care about. If the concrete type has the method
// named temporary then this is what we want. We get to stay decoupled and continue to work at the
// interface level.
type temporary interface {
Temporary() bool
}
// BehaviorAsContext shows how to check for the behavior of an interface
// that can be returned from the net package.
func (c *client) BehaviorAsContext() {
for {
line, err := c.reader.ReadString('\n')
if err != nil {
switch e := err.(type) {
// We can reduce 3 cases into 1 by asking in the case here during type assertion: Does
// the concrete type stored inside the error interface also implement this interface.
// We can declare and leverage that interface ourselves.
case temporary:
if !e.Temporary() {
log.Println("Temporary: Client leaving chat")
return
}
default:
if err == io.EOF {
log.Println("EOF: Client leaving chat")
return
}
log.Println("read-routine", err)
}
}
fmt.Println(line)
}
}
// Lesson:
// Thank to Go Implicit Conversion.
// We can maintain a level of decopling by creating an interface with methods or behaviors that we only want,
// and use it instead of concrete type for type assertion switch.