Golang学习笔记_20——error
Golang学习笔记_21——Reader
Golang学习笔记_22——Reader示例
文章目录
- error补充
- 1. 基本错误处理
- 2. 自定义错误
- 3. 错误类型判断
- 3.1 类型断言
- 3.2 类型选择
- 4. panic && recover
- 源码
error补充
1. 基本错误处理
在Go中,函数通常返回两个值:一个是预期的结果,另一个是error
类型的值。
如果函数执行过程中发生错误,error
值将不为nil
。
func divide(a, b float64) (float64, error) {if b == 0 {return 0, errors.New("division by zero")}return a / b, nil
}func error_demo_1() {// 调用 divide 函数进行除法运算result, err := divide(10, 0)if err != nil {// 处理错误println("Error:", err)} else {// 输出结果println("Result:", result)}
}
测试方法
func Test_error_demo_1(t *testing.T) {error_demo_1()
}
输出结果
=== RUN Test_error_demo_1
Error: (0x105134bc8,0x1400009ef48)
--- PASS: Test_error_demo_1 (0.00s)
PASS
2. 自定义错误
自定义错误类型可以实现error接口,该接口仅包含一个Error方法,返回一个字符串。
// 自定义错误
type MyError2 struct {When stringwhat string
}func (e *MyError2) Error() string {return fmt.Sprintf("when: %s, what: %s", e.When, e.what)
}func testMyError() error {err := &MyError2{When: "now",what: "something wrong",}return err
}
测试方法
func Test_testMyError(t *testing.T) {if err := testMyError(); err != nil {fmt.Println(err)}
}
输出结果
=== RUN Test_testMyError
when: now, what: something wrong
--- PASS: Test_testMyError (0.00s)
PASS
3. 错误类型判断
3.1 类型断言
// MyError2 是自定义错误类型func ErrorAssertDemo() error {err := &MyError2{When: "now",what: "something wrong",}return err
}func TestErrorAssertDemo() {err := ErrorAssertDemo()if specificErr := err.(*MyError2); specificErr != nil {fmt.Println("specificErr:", specificErr)} else {fmt.Println("normalErr:", err)}
}
测试方法
func Test_ErrorAssertDemo(t *testing.T) {TestErrorAssertDemo()
}
输出结果
=== RUN Test_ErrorAssertDemo
specificErr: when: now, what: something wrong
--- PASS: Test_ErrorAssertDemo (0.00s)
PASS
3.2 类型选择
// 错误类型选择
// MyError2 是自定义错误类型
func ErrorTypeDemo1() error {return &MyError2{When: "now",what: "myError wrong",}
}func ErrorTypeDemo2() error {return errors.New("normal wrong")
}func switchErrorDemo(err error) {if err != nil {switch err1 := err.(type) {case *MyError2:fmt.Println("myError2:", err1)default:fmt.Println("normal:", err1)}}
}func TestErrorTypeDemo() {err1 := ErrorTypeDemo1()err2 := ErrorTypeDemo2()switchErrorDemo(err1)switchErrorDemo(err2)
}
测试方法
func Test_ErrorTypeDemo(t *testing.T) {TestErrorTypeDemo()
}
输出结果
=== RUN Test_ErrorTypeDemo
myError2: when: now, what: myError wrong
normal: normal wrong
--- PASS: Test_ErrorTypeDemo (0.00s)
PASS
4. panic && recover
在Go中,panic
用于表示一个不可恢复的运行时错误。当panic
发生时,程序将停止正常执行,并开始逐级调用已注册的延迟函数(deferred functions),随后程序崩溃。
recover
是一个内置函数,用于从panic
中恢复。它只能在延迟函数中调用。在正常的执行路径中调用recover
将返回nil
。
// panic 和 recover
func myPanic() {panic("error happened")
}func safeRecover() {defer func() {if err := recover(); err != nil {fmt.Println("recover:", err)}}()myPanic()
}
测试方法
func Test_safeRecover(t *testing.T) {safeRecover()fmt.Println("after recover")}
输出结果
=== RUN Test_safeRecover
recover: error happened
after recover
--- PASS: Test_safeRecover (0.00s)
PASS
源码
// error_demo_2.go 文件
package error_demoimport ("errors""fmt"
)func divide(a, b float64) (float64, error) {if b == 0 {return 0, errors.New("division by zero")}return a / b, nil
}func errorDemo1() {// 调用 divide 函数进行除法运算result, err := divide(10, 0)if err != nil {// 处理错误println("Error:", err)} else {// 输出结果println("Result:", result)}
}// 自定义错误
type MyError2 struct {When stringwhat string
}func (e *MyError2) Error() string {return fmt.Sprintf("when: %s, what: %s", e.When, e.what)
}func testMyError() error {err := &MyError2{When: "now",what: "something wrong",}return err
}func ErrorAssertDemo() error {err := &MyError2{When: "now",what: "something wrong",}return err
}func TestErrorAssertDemo() {err := ErrorAssertDemo()if specificErr := err.(*MyError2); specificErr != nil {fmt.Println("specificErr:", specificErr)} else {fmt.Println("normalErr:", err)}
}// 类型选择
func ErrorTypeDemo1() error {return &MyError2{When: "now",what: "myError wrong",}
}func ErrorTypeDemo2() error {return errors.New("normal wrong")
}func switchErrorDemo(err error) {if err != nil {switch err1 := err.(type) {case *MyError2:fmt.Println("myError2:", err1)default:fmt.Println("normal:", err1)}}
}func TestErrorTypeDemo() {err1 := ErrorTypeDemo1()err2 := ErrorTypeDemo2()switchErrorDemo(err1)switchErrorDemo(err2)
}// panic 和 recoverfunc myPanic() {panic("error happened")
}func safeRecover() {defer func() {if err := recover(); err != nil {fmt.Println("recover:", err)}}()myPanic()
}
// error_demo_2_test.go 文件
package error_demoimport ("fmt""testing"
)func Test_error_demo_1(t *testing.T) {errorDemo1()
}func Test_testMyError(t *testing.T) {if err := testMyError(); err != nil {fmt.Println(err)}
}func Test_ErrorAssertDemo(t *testing.T) {TestErrorAssertDemo()
}func Test_ErrorTypeDemo(t *testing.T) {TestErrorTypeDemo()
}func Test_safeRecover(t *testing.T) {safeRecover()fmt.Println("after recover")}