2020. február 3., hétfő 17:13:26 UTC+1 időpontban Craig Rodrigues a következőt írta: > > > > 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> >> 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 should do as few as possible in init() functions - use them only when all else fails! They're are convenience hacks, but an "import with a side effect" goes against Go's philosophy of explicitness.
Be explicit and avoid magic - so put your flag.Parse() into your main() function! Tamás Gulácsi -- 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/8e0b6483-839b-4235-9506-c42855e1b24a%40googlegroups.com.