Quantcast
Channel: Defer to outside a function - Stack Overflow
Browsing all 3 articles
Browse latest View live

Answer by Burak Serdar for Defer to outside a function

One way to do this is using an "initializer" function with callback:func WithResources(f func(Resource1, Resource2)) { r1:=NewResource1() defer r1.Close() r2:=NewResource2() defer r2.Close()...

View Article


Answer by Volker for Defer to outside a function

It would be wonderful to wrap all this in a function.Most probably a lot of people would hate reading such code. So "wonderful" might be very subjective.However doing so would close the resource as...

View Article

Defer to outside a function

A common pattern I use is:resource.open()defer resource.close()sometimes checking errors in between, which leads to:err := resource.open()if err != nil{ //do error stuff and return}defer...

View Article
Browsing all 3 articles
Browse latest View live