golang/go

cmd/vet: reject flag.Parse during func init

Open

#33,190 opened on Jul 19, 2019

View on GitHub
 (4 comments) (12 reactions) (0 assignees)Go (19,008 forks)batch import
AnalysisNeedsFixearly-in-cyclehelp wanted

Repository metrics

Stars
 (133,883 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

In #31859, @cespare suggested rejecting flag.Parse during func init, which is always incorrect (other packages not yet initialized may want to define flags).

We could add a special runtime hook of some kind to allow flag to see whether main.main has started, but that would be unfortunate. There also may be lots of code in the wild that does parse flags during init and kind of works out OK, and if it's working well enough we don't want to break it unnecessarily.

A vet check, on by default during go test, seems like the perfect compromise to me.

Contributor guide