有沒有更好的方法來做到這一點?我需要知道v的類型是否是內置的「錯誤」類型。我覺得應該有這樣做的更合適的方法:Go AST/Types - 如何分辨錯誤?
import (
"go/ast"
"go/types"
)
func IsError(v ast.Expr, info types.Info) bool {
t := info.Types[v]
return t.Type.String() == "error" &&
t.Type.Underlying().String() == "interface{Error() string}"
}
請添加一個可運行的鏈接。例如使用https://github.com/plutov/playgo – pltvs
下面是一個可運行的示例:https://play.golang.org/p/MrhlFdBN3w –
我覺得這個就足夠了: return info.Types [v] .Type.String()==「錯誤」 – pltvs