-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_sentinel.go
61 lines (48 loc) · 1.8 KB
/
error_sentinel.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
package errs
import (
"errors"
"fmt"
)
var (
// ErrSentinel -- sentinel error check
ErrSentinel = errors.New("example: sentinel error check")
)
// Sentinel は、Goにおけるエラー処理イディオムの sentinel error check についてのサンプルです.
// REFERENCES::
// - https://medium.com/onefootball-locker-room/a-look-at-go-1-13-errors-9f6c9f6accb6
func Sentinel() error {
// ----------------------------------------------------------------
// Sentinel error check
//
// Sentinelとは「衛兵」とか「ガード」の意味。
// 予め、外部変数としてエラー変数を定義しておいて、発生したエラーをその変数の値と比べて
// 一致している場合は、対象のエラーが発生していると見なすチェック方法.
//
// Go 1.13 から、 errors.Is() が追加されたので、従来では == を使って判定していた
// 部分を errors.Is() で判定できるようになった。
// ----------------------------------------------------------------
raise := func() error {
return ErrSentinel
}
if err := raise(); err != nil {
// Go 1.13 より前のバージョンでは == で比較していた
if err == ErrSentinel {
fmt.Printf("Go 1.13 より前 (==で判定): %v\n", err)
}
// Go 1.13 からは errors.Is() があるので、そちらを使った方がシンプルかつ高機能
if errors.Is(err, ErrSentinel) {
fmt.Printf("Go 1.13 以降 (errors.Is()で判定): %v\n", err)
}
}
return nil
/*
$ task
task: [build] go build .
task: [run] ./try-golang -onetime
ENTER EXAMPLE NAME: error_sentinel
[Name] "error_sentinel"
Go 1.13 より前 (==で判定): example: sentinel error check
Go 1.13 以降 (errors.Is()で判定): example: sentinel error check
[Elapsed] 7.42µs
*/
}