-
Notifications
You must be signed in to change notification settings - Fork 18k
It should be possible to use command line parameters extracted via flag.Parse within the init()-ization function of all packages #39093
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
Comments
The pattern you describe is not recommended in Go. It can only work if the package P1 that calls This is not an artificial limitation. It's inherent in the design of the flag package. Multiple calls to We aren't going to change the flag package or the testing package. For your use case, consider using a different flag package. |
First I would like to mention that I don't want to go against the current or against the best technical model. I don't want to challenge anything. That being said: Question/Problem: How would it then be correct to have packages use in their init() functions values that come from the cli? This is a simple question. What I was (maybe naively) suggesting was that each package could register its set of flags it would be interested in and call Parse on its own to discover the values of the flags registered so far. This would not rely on the pattern of imports you describe above since each interested package could not only register flags, but try to fetch those already registered. Obviously, the Parse caller is interested in the flags it just defined moments before, not in the flags that any different package may define for its own use later. Is there another way for the question above except using something totally different than Parse? Thanks for your patience, hope this explains it a tad better. |
With the syntax used by the standard library flags package, you can't parse flags if you don't know all the flags, because when you see You can't know all the flags in an Of course, an There's nothing with doing things that way if you like. It's just not how the standard library flags package works, and it's not possible for the standard library flags package to work that way. |
Ok, thanks for the explanation. It does make sense, even if I don't like the limitation per se :) |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Since the changes that are most extensively discussed here #31859
it is no longer possible to use parameters that come from the cli parsing within the init() function of dependent packages because it breaks the testing package.
It seems that it is advised to only call flag.Parse from within the main function of the application.
Before, we were using a singleton object created in a dedicated configuration package. This package parsed the cli parameters as well as some environment variables within its own init() function and created the singleton config object. Most other packages from the application would import the configuration package and access the config singleton. This worked well because the configuration package was first to execute and the other packages could in turn execute whatever complex initialization code they would need using parameters from the cli that were already available.
Now, this pattern is no longer possible, basically there is no straightforward way to use the cli parameters from within the init function of application packages. This seems an artificial limitation since if one would use environment variables for configuration, this would be possible. But not with cli parameters.
What did you expect to see?
Be able to use flag.Parse within init()
What did you see instead?
#31859
Discussion
Is this pattern I described not recommended in Go?
I feel that this is an artificial limitation, especially if one considers that the above pattern could be easily implemented with environment variables but not with cli flags.
Wouldn't be possible to address this limitation with a better implementation in the flag package that would allow multiple calls to flag.Parse() ? Or maybe separate the testing flags parsing out so it does not interfere with the application's own call to flag.Parse in an init() function?
The text was updated successfully, but these errors were encountered: