引
这篇是对 errors 包 的姿势挖掘
气功波错误代码
从 http.Get()返回的错误 判断 syscall.ECONNREFUSED 错误.以前要对 go 标准库 error 结构有点熟悉,才能写出下面的代码
func CmdErr(err error) {
if err == nil {
return
}
if uerr, ok := err.(*url.Error); ok {
if noerr, ok := uerr.Err.(*net.OpError); ok {
if scerr, ok := noerr.Err.(*os.SyscallError); ok {
if scerr.Err == syscall.ECONNREFUSED {
fmt.Printf("gurl: (7) couldn't connect to host\n")
return
}
}
}
}
fmt.Printf("%s\n", err)
}
用 go1.13 errors 库重构气功波代码
现在只要知道,syscall.ECONNREFUSED 就行。errors.Is直接判断
func newCmdErr2(err error) {
if errors.Is(err, syscall.ECONNREFUSED) {
fmt.Printf("gurl: (7) couldn't connect to host\n")
return
}
}
网友评论