↧
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 ArticleAnswer 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 ArticleDefer 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
More Pages to Explore .....