平面设计 网站推荐,gzip 网站,网站开发公司计划书,一叶子电子商务网站建设策划书Golang学习笔记_20——error Golang学习笔记_21——Reader Golang学习笔记_22——Reader示例 文章目录 error补充1. 基本错误处理2. 自定义错误3. 错误类型判断3.1 类型断言3.2 类型选择 4. panic recover 源码 error补充
1. 基本错误处理
在Go中#xff0c;函数…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)
PASS2. 自定义错误
自定义错误类型可以实现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)
PASS3. 错误类型判断
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)
PASS3.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)
PASS4. 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 (errorsfmt
)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 (fmttesting
)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)}