-
Notifications
You must be signed in to change notification settings - Fork 83
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
Add option to generate a function rather than global variable #88
Comments
Can you please expand a bit on the motivation? What would you do differently if this were implemented? Is this a feature request to be able to defer the cost of initialization of the virtual filesystem to happen on first call, rather than at program start up time? |
Hmm, to be honest, my main motivation is only following Go best practices, which recommends avoid global variables. Also, I would like to understand the reasoning behind chosing: var vfsgenAssets = func() http.FileSystem { over func vfsgenAssets() http.FileSystem { Are they exactly the same? |
The best practice recommends avoiding global variables when it is possible and when it makes sense. It doesn't seem very applicable in this situation. To answer your question, let me be more precise. If you're asking if there's a difference between: var vfsgenAssets = func() http.FileSystem {
...
}() Which is the code func vfsgenAssets() http.FileSystem {
// ...
} Then they are not the same. The In the second block, (If |
Oh, I didn't notice that this anonymous function is actually called at the end! In this case, it definitely make sense to make it a function IMO, as currently
Good point, TIL. |
It would be great to have another field in
Options
struct calledFunctionName
, which would generate a function returninghttp.FileSystem
, rather than a function assigned to global variable, as globals are magic and should not be used whenever possible (though a function in global is not that bad 😄).The text was updated successfully, but these errors were encountered: