Is it possible to put defer
in sub function?
I.e., for line 12~16 of func test1()
in
https://play.golang.org/p/evabhcjvNs (enclosed below)
Is there any possibility to put them in sub function?
Like what I tried in test2()
(but that behavior is different than test1()
).
The reason I'm asking is that, for line 12~16 of func test1()
, my actual code is to restore the variable from persistent data, then use defer
to save it when test1()
is done. However, there are cases that the whole restore/save is not necessary, so I'm thinking a better way to control it.
thanks
package main
import "log"
func main() {
test1()
log.Printf("==== \n")
test2()
}
func test1() {
r, err := Open("a")
if err != nil {
log.Fatalf("error opening 'a'\n")
}
defer r.Close()
r.Use()
}
func test2() {
r := subc()
r.Use()
}
func subc() *Resource {
r, err := Open("a")
if err != nil {
log.Fatalf("error opening 'a'\n")
}
defer r.Close()
return r
}
type Resource struct {
name string
}
func Open(name string) (*Resource, error) {
log.Printf("opening %s\n", name)
return &Resource{name}, nil
}
func (r *Resource) Use() error {
log.Printf("using %s\n", r.name)
return nil
}
func (r *Resource) Close() error {
log.Printf("closing %s\n", r.name)
return nil
}
I think I understand what you're asking. You want to know if a function can place a function on the defer stack of the caller. The answer to that is no. One possible solution to this is to have the function that wants to defer something return that function to the caller and have the caller do the defer
. For example:
func test2() {
r, cleanup := subc()
defer cleanup()
r.Use()
}
func subc() (*Resource, func()) {
r, err := Open("a")
if err != nil {
log.Fatalf("error opening 'a'\n")
}
return r, func() { r.Close() }
}