Skip to content
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

Go 1.13+: one shall not call flag.Parse() in init #309

Closed
eclipseo opened this issue Jan 31, 2020 · 2 comments
Closed

Go 1.13+: one shall not call flag.Parse() in init #309

eclipseo opened this issue Jan 31, 2020 · 2 comments

Comments

@eclipseo
Copy link

eclipseo commented Jan 31, 2020

See bug golang/go#33475 and bug golang/go#31859 for the rationale

To be clear, we are talking only about flag.Parse here, not flag.*. The flag package is designed so that multiple packages can define flags, without those packages being aware of each other. It's essential that flag.Parse be called only after all flags are defined, meaning only after all packages are initialized. The normal practice is that the main package calls flag.Parse, because the main package is the only package that is sure to run after all packages are initialized.

Your program may well be designed so that it is OK to call flag.Parse in an init function of some non-main package. But most programs do not and cannot work that way. So I am questioning how much we should twist the testing package to support that use case. Of course if the change to the testing package is simple, then that is fine. But I would not like to see a lot of complexity added to support a very unusual use case.

@eclipseo
Copy link
Author

eclipseo commented Jan 31, 2020

See golang/go#31859 (comment) for a workaround.

phaus added a commit to inspired-consulting/martian that referenced this issue Mar 14, 2022
@phaus
Copy link
Contributor

phaus commented Mar 14, 2022

I just pushed a small update, that helps using this module with go > 1.13.

phaus added a commit to inspired-consulting/go-replayers that referenced this issue Mar 14, 2022
jba added a commit to google/go-replayers that referenced this issue Mar 14, 2022
bramhaghosh pushed a commit that referenced this issue Aug 16, 2022
* moving flag.Parse() from init to main function(s) fixes #309

* updating go to version 1.18, moving init code into main function

* updating go to version 1.18, moving init code into main function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants