New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove defer Statement #10
Conversation
This small patch removes a defer statement inside of a for loop and calls Close after the scanner is done reading the file.
Thanks for PR, @gsquire! I'm not sure however if this is fixing or improving anything. Can you elaborate? Edit: I guess it would make a difference if you got a larger amount of dictionaries and want to prevent keeping the file handles open. Makes sense. |
Thank you! |
You're welcome! My reasoning was that the |
Hah, I see you've even written your own little detector: https://github.com/gsquire/dll Kudos, nice work! |
Thanks, hope you can find it useful :) |
Note: I don't think that would have been an issue here, as Also, unless I'm entirely mistaken, this should also only be a problem if the actual loop-value were to be evaluated in the deferred call. For example, this isn't a problem: package main
import "fmt"
type foo struct {
name string
}
func (f foo) print() {
fmt.Println(f.name)
}
func main() {
var f []foo
f = append(f, foo{name: "a"})
f = append(f, foo{name: "b"})
f = append(f, foo{name: "c"})
for _, v := range f {
defer v.print()
}
} Output (as expected):
This however would lead to problems: package main
import "fmt"
type foo struct {
name string
}
func (f foo) print() {
fmt.Println(f.name)
}
func main() {
var f []foo
f = append(f, foo{name: "a"})
f = append(f, foo{name: "b"})
f = append(f, foo{name: "c"})
for _, v := range f {
defer func() {
v.print()
}()
}
} Output:
Maybe that's something to take into account for |
Yeah, I think the idea is that they could pile up in something like a server with an infinite loop for example. It'd be hard to check that semantically within the tool but it's something cool to explore. There is a way around it as well. |
This small patch removes a defer statement inside of a for loop and calls
Close
after the scanner is done reading the file.