|
|
|
|
|
by harikb
1942 days ago
|
|
Came here to note this exact difference with respect to finally Consider this code fp = os.open(x) // imagine file open
defer fp.close()
fp.read()
With defer, one would think I can simply wrap this in a for-loop if I want to open and read a bunch of files. Go doesn’t promise this, but not clear until linter complains. In languages were it “ends at scope”, this is still wrong. If we wrote it as finally, dev would know finally is outside the loop or they need to wrap in another sub-scope inside the loop {} |
|
I don’t quite follow -- what would go wrong? If you put that in a loop body, won’t you get one open() and one close() per iteration, as intended?