golang 的runtime庫,提供Caller函數,能夠返回運行時正在執行的文件名和行號:golang
func Caller(skip int) (pc uintptr, file string, line int, ok bool) {
Caller reports file and line number information about function invocations on the calling goroutine's stack. The argument skip is the number of stack frames to ascend, with 0 identifying the caller of Caller. (For historical reasons the meaning of skip differs between Caller and Callers.) The return values report the program counter, file name, and line number within the file of the corresponding call. The boolean ok is false if it was not possible to recover the information.ide
調用方法以下,返回的file爲絕對路徑,line爲行號。有了這個就能夠在本身的日誌等函數中添加這個記錄了。函數
_, file, line, ok := runtime.Caller(1)