On Friday, January 31, 2020 at 7:23:32 AM UTC-8, Ian Lance Taylor wrote: > > > Calling flag.Parse in an init function never worked reliably, unless > you took special care. Flags are themselves often defined in init > functions, so calling flag.Parse in an init function will see the > flags that were defined before the point of the call but not the flags > defined after the point of the call. I guess it was working for you, > but small changes in your code could have caused it to stop working. > We didn't document that calling flags.Parse in an init function didn't > work reliably because we didn't realize that anybody would ever do it. > The documentation always just said "After all flags are defined, call > flag.Parse", and "Must be called after all flags are defined and > before flags are accessed by the program" without spelling out that if > you call it in an init function you won't necessarily be calling it > after all flags are defined. > > The change to the testing package changed exactly when the testing > flags are defined with respect to init functions, and it caused many > cases of calling flag.Parse from an init function to break. But those > cases were already fragile and could have been broken in other ways > also. > > If you don't call flag.Parse from an init function you will be > following the documentation, and that will continue to work. > > Sorry for the trouble. > >
Thanks for the response. I have a few questions. 1. In https://golang.org/pkg/flag/#Parse , would it be appropriate to add a warning to not call this function from inside init()? This was not obvious to me from reading the docs. 2. To fix my problem, I removed flag.Parse() completely. Is that the appropriate fix? Where would it be appropriate for me to call flag.Parse()? Or should I never bother calling it (inside test files)? 3. Is calling flag.Parse() from inside https://golang.org/pkg/testing/#Init supposed to work? I tried that and it also did not work. Thanks again. -- Craig -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/6300b452-f67a-4a17-9c7d-fa2f9feb82ee%40googlegroups.com.