New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/compile: __SOURCE__ constant like __FILE__, __LINE__, __FUNCTION__ C++ macro #37620
Comments
You can get all of this information by calling We could choose to implement |
i think if the string generated at compile time (then sent to the other function) would be more efficient than inspecting stack trace at runtime. |
I'm saying that if this seems important then we could implement It's not obvious to me that we need two different ways to get the same information. |
but i'm really not sure if we could get info about the caller at compile time '__') |
The compiler can trivially compute the file and line results of It takes a little more work to trace the PC result passed into |
I mean, if there's function A and B that calls function C, wouldn't that be require runtime instead of compile time?
also if using
|
func A() {
_, source, _, _ := runtime.Caller(0)
B(source)
} |
runtime.Caller is expensive |
There's an element of this that isn't only about performance. This would be helpful for quick insert print style debugging. I know we have delve and other tools, but sometimes print style debugging ftw. The fact that func A() {
{
_, file, line, _ := runtime.Caller(0)
fmt.Printf("%s:%d i was here", file, line)
}
// ...
{
_, file, line, _ := runtime.Caller(0)
fmt.Printf("%s:%d i was here", file, line)
}
// ...
}
func A() {
fmt.Printf("%s:%d i was here", __FILE__, __LINE__)
// ...
fmt.Printf("%s:%d i was here", __FILE__, __LINE__)
// ...
} I'm not sure this is compelling enough, but I know I'd definitely use it if it existed. |
those macros are very useful for debugging and caching:
usage example
on code above, if there's sql error, we can know who's the caller directly if there's SQL error (without needing to access
runtime.Stack/FuncForPC
(which i currently use), this is a bit similar to C#[System.Runtime.CompilerServices.CallerMemberName], [System.Runtime.CompilerServices.CallerFilePath], [System.Runtime.CompilerServices.CallerLineNumber]
but works on compile time instead of runtime.The text was updated successfully, but these errors were encountered: