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 #3

Closed
eclipseo opened this issue Aug 5, 2019 · 3 comments
Closed

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

eclipseo opened this issue Aug 5, 2019 · 3 comments

Comments

@eclipseo
Copy link

eclipseo commented Aug 5, 2019

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.

I've read that you don't do much Go anymore, but it would be great if you could keep your package functional for Go 1.13 and further.

karlseguin pushed a commit that referenced this issue Aug 6, 2019
@karlseguin
Copy link
Owner

karlseguin commented Aug 6, 2019

You're right. I haven't done Go in a while. Do you mind taking a look at:
https://github.com/karlseguin/expect/blob/no_init/runner.go#L40

Basically move the init logic into the call to Expectify, guarded by a mutex (probably unecessary, but better safe than sorry I think?)

@eclipseo
Copy link
Author

eclipseo commented Aug 7, 2019

I'm not a Go developer only a packager so I can't tell if it's good or not.

@karlseguin
Copy link
Owner

Thanks, I merged. Should be fixed.

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