我能够用juju/errors打印错误的行号,但不知道如何用pkg/errors做同样的事情。
package main
import (
jerrors "github.com/juju/errors"
perrors "github.com/pkg/errors"
"io/ioutil"
"log"
)
func jerror() error {
//throw an error....
_, err := ioutil.ReadDir("r")
if err != nil {
return jerrors.Trace(err)
}
return nil
}
func perror() error {
//throw an error....
_, err := ioutil.ReadDir("r")
if err != nil {
return perrors.Cause(err)
}
return nil
}
func main() {
jerr := jerror()
if jerr != nil {
log.Println(jerrors.ErrorStack(jerr))
}
log.Println("-------------------------")
perr := perror()
if perr != nil {
log.Println(perrors.WithStack(perr))
}
}
打印出来:
2020/08/26 00:19:48 open r: no such file or directory
go-mock-json-api/main.go:15:
2020/08/26 00:19:48 -------------------------
2020/08/26 00:19:48 open r: no such file or directory
ibeautiful
相关分类