go error 简单实现
- 高效处理golang的error, 避免处理大量的
err!=nil判断 - 高效处理golang的recover, 让错误中包含丰富的堆栈信息
- xerror实现标准As,Is,Unwrap接口, 可以和其他error库一起使用
- 简单易用
go test -bench=. -benchmem -memprofile memprofile.out -cpuprofile profile.out ./...
go tool pprof -http=":8081" profile.out
go tool pprof -http=":8081" memprofile.outgoos: darwin
goarch: amd64
pkg: github.com/pubgo/xerror
BenchmarkPanic-8 1353934 883 ns/op 128 B/op 2 allocs/op
BenchmarkPanicWithOutCaller-8 3861938 309 ns/op 48 B/op 1 allocs/op
BenchmarkNoPanic-8 201641330 5.87 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/pubgo/xerror 4.363spackage xerror_test
import (
"fmt""testing""github.com/pubgo/xerror"
)
funcTestErr(t*testing.T) {
fmt.Println(xerror.Wrap(xerror.ErrAssert))
}
funcTestParseWith(t*testing.T) {
varerr=fmt.Errorf("hello error")
xerror.ParseWith(err, func(err xerror.XErr) {
fmt.Printf("%v\n", err)
})
}
funcTestRespTest(t*testing.T) {
deferxerror.RespTest(t)
TestPanic1(t)
}
funcTestRespNext(t*testing.T) {
deferxerror.RespExit("TestRespNext")
TestPanic1(t)
}
funcTestPanic1(t*testing.T) {
//defer xerror.RespExit()deferxerror.RespRaise(func(err xerror.XErr) error {
returnxerror.WrapF(err, "test raise")
})
//xerror.Panic(xerror.New("ok"))xerror.Panic(fmt.Errorf("ss"))
}
funcinit1Next() (errerror) {
deferxerror.RespErr(&err)
xerror.Panic(fmt.Errorf("test next"))
returnnil
}
funcBenchmarkNoPanic(b*testing.B) {
fori:=0; i<b.N; i++ {
_=func() (errerror) {
deferxerror.RespErr(&err)
xerror.Panic(nil)
return
}()
}
}