command
Version:
v1.0.0
Opens a new window with list of versions in this module.
Published: Jul 25, 2025
License: Apache-2.0
Opens a new window with license information.
Imports: 3
Opens a new window with list of imports.
Imported by: 0
Opens a new window with list of known importers.
README
¶
Errors Package Demo
这个示例展示了 Go-Kit 错误包的各种使用方法和最佳实践。
运行示例
# 在项目根目录下运行
go run examples/errors-demo/main.go
示例内容
- 基本错误创建 - 展示如何创建不同类型的错误
- 错误包装 - 演示如何包装现有错误
- 错误检查 - 展示错误类型检查方法
- 链式调用 - 演示流畅的API设计
- 格式化错误 - 展示格式化字符串创建错误
- 工具函数 - 演示各种工具函数的使用
- 便利函数 - 展示快捷创建常用错误的方法
- 实际应用场景 - 模拟真实业务场景中的错误处理
输出示例
运行示例后,你将看到类似以下的输出:
=== Go-Kit Errors Package Demo ===
1. 基本错误创建
----------------
默认消息错误: [INVALID_PARAM] 参数无效
自定义消息错误: [NOT_FOUND] 用户不存在
带详细信息错误: [DATABASE_ERROR] 数据库连接失败: 连接超时
2. 错误包装
------------
包装错误: [NOT_FOUND] 资源不存在
包装带详细信息: [DATABASE_ERROR] 数据库操作失败: SQL执行超时
3. 错误检查
------------
err1 是参数错误: true
err1 是未找到错误: false
err2 是未找到错误: true
err3 是数据库错误: true
err1 的错误码: INVALID_PARAM (代码: 1001)
...
学习要点
- 错误码系统的使用
- 错误包装和链式调用
- 上下文信息的添加
- 错误类型的安全检查
- 格式化错误的创建
- 实际业务场景的应用
相关文档
Documentation
¶
There is no documentation for this package.
Source Files
¶
Click to show internal directories.
Click to hide internal directories.