I want to know the return values at time of exit from a golang function. The golang defer mechanism is helpful, but it evaluates arguments at the time the defer statement is registered rather than when it is executed. I can work with this using an anonymous function which accesses the return values:
func try() (int i) {
defer func() {fmt.Printf("%d", i)}()
i = 10
return i+1
}
func main() {
try()
}
This would work ok, i think, but I would like to handle this in a generic manner, perhaps something like:
func try(in string) (out int) {
enter("%s", in);exit("%d", out)
}
or, even better, use reflection to output the arguments/return values at time of entry/exit. I'm assuming runtime performance isn't critical :).
Is there a good way to do this? Shaba Abhiram's handy Tracey lib does go a long way towards this but stops short of printing the return values.
Aucun commentaire:
Enregistrer un commentaire