Yes @Jan Mercl  absolutely correct!!

Able to figure out a solution.
Basically the go mod i am using as part of the code is doing something like 
this :
func init() {
//reads vars
flag.Parse()
}

The problem arises when doing flag.Parse() as part of the init.
https://github.com/golang/go/issues/31859#issuecomment-489889428 gives very 
valuable insights to the issue.

High Level Problems that may occur :
1. if i am reading any command line args anywhere else it will fail, since 
in this code flag.Parse() is happening during init()
2. The code snippet :
*var _ = func() bool {*
*testing.Init()*
*return true*
*}()*
This somewhat of a hack to explicitly invoke init() method and only helps 
in a very few cases.

As a solution, i went ahead to remove the flag.Parse() from init() because 
it is really wrong and rather call it from my main().
Just doing this change unblocked me for testing as now from test method i 
can easily invoke a setup call to do flag.Parse() as Jan Mercl pointed out.

On Sunday, April 25, 2021 at 2:27:15 AM UTC+5:30 Jan Mercl wrote:

> On Sat, Apr 24, 2021 at 10:37 PM Shivendra Singh
> <shivendra...@gmail.com> wrote:
>
> > ..., now in the init() method it parses flag to read variables such as 
> env and app name that is required to load the appropriate config.
>
> IINM, calling flag.Parse anywhere else than in main() or TestMain() is
> not really compatible with what go test relies on.
>
> flag.Parse changes shared state, doing it without coordination from
> different places in order that might be not under the control of the
> package that does it in its init() may create a mess that would be
> consistent with your observation.
>

-- 
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/627bfc28-dbe6-4bd8-9b9a-34ed78a04f24n%40googlegroups.com.

Reply via email to