I was trying to throw errors in my Golang program with log.Fatal
but, log.Fatal
does not also print the line where the log.Fatal
was ran. Is there no way of getting access to the line number that called log.Fatal? i.e. is there a way to get the line number when throwing an error?
I was trying to google this but was unsure how. The best thing I could get was printing the stack trace, which I guess is good but might be a little too much. I also don't want to write debug.PrintStack()
every time I need the line number, I am just surprised there isn't any built in function for this like log.FatalStackTrace()
or something that isn't costume.
Also, the reason I do not want to make my own debugging/error handling stuff is because I don't want people to have to learn how to use my special costume handling code. I just want something standard where people can read my code later and be like
"ah ok, so its throwing an error and doing X..."
The less people have to learn about my code the better :)
Short version,
there's nothing directly built in, however you can implement it with a minimal learning curve usingruntime.Caller
playground
You can set the Flags on either a custom Logger, or the default to include
Llongfile
orLshortfile