GO自定义Error方法

通过interface定义

1
2
3
type error interface {
Error() string
}

自定义error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
type Error struct {
errCode uint8
}
func (e *Error) Error() string {
switch e.errCode {
case 1:
return "file not found"
case 2:
return "time out"
case 3:
return "permission denied"
default:
return "unknown error"
}
}