You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is actually a feature request -- not a bug.
I like the defer statements Go has to offer. Still, I think it would be nice if the
language also offered something like loop-local defer statements:
for _, f := range files {
fd := open(f)
defer close(fd)
...
}
The disadvantage of this piece of code would be that it only closes all the file
descriptors at the end of the invocation of the function. We already have label scopes,
so technically speaking there is room to allow something like:
MyLoop: for _, f := range files {
fd := open(f)
defer MyLoop close(fd)
...
}
Especially when interacting with C code it would be quite comfortable to have (e.g. call
free() at the end of a loop iteration).
Any thoughts?
The text was updated successfully, but these errors were encountered:
You can already Go code to act as you wish:
for _, f := range files {
func() {
fd := open(f)
defer close(fd)
...
}()
}
So the only question here is whether the functionality you want is useful enough to add
new syntactic sugar. I don't think it rises to that level. (And of course nothing will
change in Go 1.)
by edje@google.com:
The text was updated successfully, but these errors were encountered: