You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I often need to see what's really going on in a specific function, so I drop a quick
`fmt.Println` in the code to output the data I want to investigate.
This has 2 disadvantages:
1) I always need to jump to the header and import "fmt" and remove it when the
log is removed
2) If I need more context information, I also have to import "runtime" to get
a call stack trace
I propose a built in function
trace(args...interface{})
and/or
tracef(format string, args...interface{})
that does this without requiring fmt or runtime. The output would look like the `panic`
stack trace with the custom information attached.
The text was updated successfully, but these errors were encountered:
No need to put _into the_ language a new built-int for what can be _expressed by the_
language already. #Reject
----
package foo_test
import (
"fmt"
"path"
"runtime"
)
func trace(s string, va ...interface{}) {
_, fn, fl, _ := runtime.Caller(1)
fmt.Printf("%s:%d: ", path.Base(fn), fl)
fmt.Printf(s, va...)
fmt.Println()
}
----
- Now you can write debug prints, to be printed during tests, like
trace("%v %i", myVar, i)
- No need to import/unimport anything.
- You cannot forget to remove the debug prints before building the binary, because as
long as they are present in the source, the build will fail. It will build only when
doing
$ go test
a) this solution only works provided that you are in a test case
b) you'll have to duplicate this for every package if you don't want explicit imports
So it's not a solution.
The text was updated successfully, but these errors were encountered: