package e import ( "fmt" "path" "runtime" ) type E struct { Code int // 错误码 msg string // 报错代码 st string // 堆栈信息 } func NewErr(code int, msg string) error { return E{code, msg, stack(3)} } func (e E) Error() string { return e.msg } func stack(skip int) string { stk := make([]uintptr, 32) str := "" l := runtime.Callers(skip, stk[:]) for i := 0; i < l; i++ { f := runtime.FuncForPC(stk[i]) name := f.Name() file, line := f.FileLine(stk[i]) str += fmt.Sprintf("\n%-30s[%s:%d]", name, path.Base(file), line) } return str }