How can I get a reference to a defer function?
This article states: "A defer statement pushes a function call onto a
list." I'm wondering if I can access the elements in that list from
another place in my program and then invoke them? Can I invoke them
multiple times? I'm assuming that I have a reference to the function that
has defer behavior (if that helps).
So, here's a short example of what I want to do:
func main {
doStuff = func() {
// open database connections
// write temporary files
// etc...
defer func() {
// close database connections
// delete temporary files
// etc...
}()
}
AwesomeApplication(doStuff)
}
func AwesomeApplication(doStuff func()) {
// Now, can I get a reference to the defer function within `doStuff`?
// No, I can't just define the defer function somewhere an pass it
// with `doStuff`. Think of this as a curiosity I want to satisfy,
// not a real use case.
}
No comments:
Post a Comment