On Friday, January 31, 2020 at 1:56:49 PM UTC-8, Ian Lance Taylor wrote: > > On Fri, Jan 31, 2020 at 10:33 AM Craig Rodrigues <crod...@gmail.com > <javascript:>> wrote: > > > > 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. > > Documentation should normally be precise and not overly long. I > personally don't feel that an explicit comment about calling > flag.Parse from an init function carries its weight. The > documentation is already clear that you must only call Parse after > defining all flags, and as noted previously you have limited control > over initialization order. I think that more useful would be to > implement https://golang.org/issue/33190 (contributions welcome). > > > > 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)? > > It's not necessary to call flag.Parse in a test. The test harness > will call it for you. It's OK to call flag.Parse from a TestMain > function, and in fact that is required if TestMain is going to change > its behavior based on a flag (this is documented at > https://golang.org/pkg/testing/#hdr-Main). Broadly speaking, the only > other place that Go code should call flag.Parse is in the main > function, though of course there can be specific reasons to call it > from other places. > > > > 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. > > That is a function in the standard library, so I'm not sure what you > are asking. Are you changing the standard library? There shouldn't > be a need to do that in order to parse flags. > > Oh, I misread the documentation. I thought that Init() was a method that I was supposed to implement in my _test.go file, but I realize now that I made a mistake and that testing.Init() is a method inside the testing package.
Regarding the fact that I should not call flag.Parse() from inside an init() method in my_test.go file, if a note for this is not added to the documentation at https://golang.org/pkg/flag/#Parse , how would an end user know about this? This detail is buried deep, and the only way to understand this problem was to read the release notes for golang 1.13 and to ask on this mailing list. -- 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/151d6c15-cc90-49d5-8375-b7bd4f85dcf6%40googlegroups.com.