Golang编程模式:错误处理
包装错误
if err != nil {
return fmt.Errorf("something failed: %v", err)
}type authorizationError struct {
operation string
err error // original error
}
func (e *authorizationError) Error() string {
return fmt.Sprintf("authorization failed during %s: %v", e.operation, e.err)
}type causer interface {
Cause() error
}
func (e *authorizationError) Cause() error {
return e.err
}最佳实践
参考文章
Last updated