Golang学习笔记_17——方法
Golang学习笔记_18——接口
Golang学习笔记_19——Stringer
文章目录
- error
- 1. 接口
- 2. 创建
- 3. 自定义错误
- 4. 处理错误
- 5. 实现`Error`接口
- 源码
error
在Go语言中,error 是一个内建的接口类型,用于表示和处理错误情况。它是Go语言错误处理机制的核心。下面将详细讲解Go语言中 error 的相关概念和用法。
1. 接口
error
接口非常简单,仅包含一个方法
type error interface {Error() string
}
2. 创建
import ("errors""fmt"
)func demo_1() {err := errors.New("something went wrong")if err != nil {fmt.Println("Error:", err)}
}
测试方法
func Test_demo_1(t *testing.T) {demo_1()
}
输出结果
=== RUN Test_demo_1
Error: something went wrong
--- PASS: Test_demo_1 (0.00s)
PASS
3. 自定义错误
type MyError struct {Code intMessage string
}func (e *MyError) Error() string {return fmt.Sprintf("code:%d \nmessage:%s", e.Code, e.Message)
}func demo_2() {err := &MyError{Code: 1,Message: "something went wrong",}fmt.Println(err)
}
测试方法
func Test_demo_2(t *testing.T) {demo_2()
}
输出结果
=== RUN Test_demo_2
code:1
message:something went wrong
--- PASS: Test_demo_2 (0.00s)
PASS
4. 处理错误
在自定义错误代码的基础上,实现错误处理
func demo_3(err *MyError) {if err != nil {fmt.Println("Error:", err)} else {fmt.Println("No error")}
}
测试方法
func Test_demo_3(t *testing.T) {type args struct {err *MyError}tests := []struct {name stringargs args}{{name: "error",args: args{err: &MyError{Code: 1,Message: "something went wrong",},},},{name: "no error",args: args{err: nil,},},}for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {demo_3(tt.args.err)})}
}
输出结果
=== RUN Test_demo_3
=== RUN Test_demo_3/error
Error: code:1
message:something went wrong
=== RUN Test_demo_3/no_error
No error
--- PASS: Test_demo_3 (0.00s)--- PASS: Test_demo_3/error (0.00s)--- PASS: Test_demo_3/no_error (0.00s)
PASS
5. 实现Error
接口
// 定义一个表示文件错误的结构体
type FileError struct {FileName stringErrMsg string
}// 实现 error 接口的 Error() 方法
func (e *FileError) Error() string {return fmt.Sprintf("file error: %s - %s", e.FileName, e.ErrMsg)
}func demo_4() {err := &FileError{FileName: "example.txt",ErrMsg: "failed to open file",}fmt.Println(err)
}
测试方法
func Test_demo_4(t *testing.T) {demo_4()
}
输出结果
=== RUN Test_demo_4
file error: example.txt - failed to open file
--- PASS: Test_demo_4 (0.00s)
PASS
源码
// error_demo.go 文件
package error_demoimport ("errors""fmt"
)func demo_1() {err := errors.New("something went wrong")if err != nil {fmt.Println("Error:", err)}
}type MyError struct {Code intMessage string
}func (e *MyError) Error() string {return fmt.Sprintf("code:%d \nmessage:%s", e.Code, e.Message)
}func demo_2() {err := &MyError{Code: 1,Message: "something went wrong",}fmt.Println(err)
}func demo_3(err *MyError) {if err != nil {fmt.Println("Error:", err)} else {fmt.Println("No error")}
}// 定义一个表示文件错误的结构体
type FileError struct {FileName stringErrMsg string
}// 实现 error 接口的 Error() 方法
func (e *FileError) Error() string {return fmt.Sprintf("file error: %s - %s", e.FileName, e.ErrMsg)
}func demo_4() {err := &FileError{FileName: "example.txt",ErrMsg: "failed to open file",}fmt.Println(err)
}
// error_demo_test.go 文件
package error_demoimport ("errors""testing"
)func Test_demo_1(t *testing.T) {demo_1()
}func Test_demo_2(t *testing.T) {demo_2()
}func Test_demo_3(t *testing.T) {type args struct {err *MyError}tests := []struct {name stringargs args}{{name: "error",args: args{err: &MyError{Code: 1,Message: "something went wrong",},},},{name: "no error",args: args{err: nil,},},}for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {demo_3(tt.args.err)})}
}func Test_demo_4(t *testing.T) {demo_4()
}