_file_ or _line_ similar in golang

c, go

Solution

If you're using the `log` package, you can instruct the logger to prefix the entries with various information. You'll likely be most interested in the `Lshortfile` constant, which will result in prefixes along the lines of `d.go:23`. Alternatively, there is `Llongfile` which prints the file's full path (such as `/a/b/c/d.go:23`).

If you don't want to use the `log` package, you could also use `runtime.Caller()`, which is what the log package uses internally. It's not as straight forward as the C macros, but you could hide it behind a function (and specify the correct call depth). You can see how the log package is implemented for an example (line 140).

Problem

is there any function in go that is similar like `"_file_"` or `"_line_"` in go, to know who is calling a specific function during run time? In C we have the `"_file_"` line that can be called as macros. How to do this in go?

Original source

Related problems