Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

two package using flag but one works #41519

Closed
Nothing-no opened this issue Sep 21, 2020 · 1 comment
Closed

two package using flag but one works #41519

Nothing-no opened this issue Sep 21, 2020 · 1 comment

Comments

@Nothing-no
Copy link

@Nothing-no Nothing-no commented Sep 21, 2020

What version of Go are you using (go version)?

$ go version 
go version go1.14.1 windows/amd64

Does this issue reproduce with the latest release?

i have no try, but i can't find this issue on the Issues

What operating system and processor architecture are you using (go env)?

set GO111MODULE=on
set GOARCH=amd64
...
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
...

go env Output
$ go env

What did you do?

i want to use two package with two different flag to meet my need, test as follows:

package pk1

import (
	"flag"
	"fmt"
)

var pk1Flag = flag.String("pk1", "hello pk1", "test")

func init() {
	flag.Parse()
	fmt.Println(*pk1Flag)
}
package pk2

import (
	"flag"
	"fmt"
)

var pk2Flag = flag.String("pk2", "hello pk2", "test")

func init() {
	flag.Parse()
	fmt.Println(*pk2Flag)
}
package main

import (
	_ "flag/pk1"
	_ "flag/pk2"

	"fmt"
)

func main() {
	fmt.Println("main")
}
  • when i use: go run ./main.go or go run ./main.go -pk1=hhhhhh; both of them work well
 go run .\main.go
hello pk1
hello pk2
main

go run .\main.go -pk1=hhhhhh
hhhhhh
hello pk2
main
  • but when using: go run ./main.go -pk2=hhhh or go run ./main.go -pk1=p1 -pk2=p2; both of them do not work

What did you expect to see?

hello pk1
hhhh
main

or

p1
p2
main

What did you see instead?

flag provided but not defined: -pk2
Usage of ...\exe\main.exe:
  -pk1 string
        test (default "hello pk1")
exit status 2
@ALTree
Copy link
Member

@ALTree ALTree commented Sep 21, 2020

Never call flag.Parse in init( ) functions. As you just discovered, that doesn't work. See #33190. Closing here since this is not a bug.

@ALTree ALTree closed this Sep 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.