lundi 22 mai 2017

Can we get function argument/parameter values using Go

I already know how to get function name and function location:

func Trace(str string) string {
    pc := make([]uintptr, 10)  // at least 1 entry needed
    runtime.Callers(2, pc)
    f := runtime.FuncForPC(pc[0])
    file, line := f.FileLine(pc[0])
    str = Replace(str, "\n", ` `)
    return `-- ` + fmt.Sprintf("%s:%d %s:%s", file, line, f.Name() + str)
}

But how to get function argument values? For example, if I call that Trace function within:

func Test(a string, b int) {
  Trace(`demo`)
}
func main() {
  Trace(`aaaa`,123)
}

I want to get the value aaaa and 123 inside Trace function, without have to pass them manually to Trace function





Aucun commentaire:

Enregistrer un commentaire