forked from NamhoGim/gotraining-studyguide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_5.go
40 lines (32 loc) · 945 Bytes
/
error_5.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
// ------------
// Find the bug
// ------------
package main
import "log"
// customError is just an empty struct.
type customError struct{}
// Error implements the error interface.
func (c *customError) Error() string {
return "Find the bug."
}
// fail returns nil values for both return types.
func fail() ([]byte, *customError) {
return nil, nil
}
func main() {
// This set the err to its zero value.
// -----
// | nil |
// -----
// | nil |
// -----
var err error
// When we call fail, it returns the value of nil. However, we have the nil value of type
// *customError. We always want to use the error interface as the return value. The customError
// type is just an artifact, a value that we store inside. We cannot use the custom type
// directly. We must use the error interface, like so func fail() ([]byte, error)
if _, err = fail(); err != nil {
log.Fatal("Why did this fail?")
}
log.Println("No Error")
}