For example when iterating over lines of a file using the lines iterator from the standard library, does the file get closed correctly if I break the iteration early using break?
Asked
Active
Viewed 122 times
1 Answers
1
As the documentation says:
var f = open("numbers.txt")
defer: close(f)
f.write "abc"
f.write "def"
Gets translated into:
var f = open("numbers.txt")
try:
f.write "abc"
f.write "def"
finally:
close(f)
So the close(f)
will be always called after the try
code, even if it includes a break
.

xbello
- 7,223
- 3
- 28
- 41