forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example2.go
48 lines (38 loc) · 969 Bytes
/
example2.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
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// Sample program to show how to use error variables to help the
// caller determine the exact error being returned.
package main
import (
"errors"
"fmt"
)
var (
// ErrBadRequest is returned when there are problems with the request.
ErrBadRequest = errors.New("Bad Request")
// ErrMovedPermanently is returned when a 301/302 is returned.
ErrMovedPermanently = errors.New("Moved Permanently")
)
func main() {
if err := webCall(true); err != nil {
switch err {
case ErrBadRequest:
fmt.Println("Bad Request Occurred")
return
case ErrMovedPermanently:
fmt.Println("The URL moved, check it again")
return
default:
fmt.Println(err)
return
}
}
fmt.Println("Life is good")
}
// webCall performs a web operation.
func webCall(b bool) error {
if b {
return ErrBadRequest
}
return ErrMovedPermanently
}