I have seem this answer and use @OneOfOne answers. How do you get a Golang program to print the line number of the error it just called?
But there is some questions.
func FancyHandleError(err error) (b bool) {
if err != nil {
pc, fn, line, _ := runtime.Caller(1)
log.Printf("[error] in %s[%s:%d] %v", runtime.FuncForPC(pc).Name(), fn, line, err)
b = true
}
return
}
func main(){
FancyHandleError(funcA())
}
func funcA()error{
err := funcB()
return err
}
func funcB()error{
err := funcC()
return err
}
func funcC()error{
err := errors.New("deep errors!!") //I want to get the location in here!!!!!!!!!!!!!!!!!!!!!!
return err
}
It will print "[error] in main.main[/root/temp/error.go:23]" That is in function main's line number.
But how to locate the error's line number in function C()?