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

flag: define flag in multiple package does not working #31901

Closed
chinglinwen opened this issue May 8, 2019 · 3 comments
Closed

flag: define flag in multiple package does not working #31901

chinglinwen opened this issue May 8, 2019 · 3 comments

Comments

@chinglinwen
Copy link

chinglinwen commented May 8, 2019

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

$ go version
go version go1.12.5 linux/amd64

Does this issue reproduce with the latest release?

yes

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/wen/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/wen/gocode"
GOPROXY=""
GORACE=""
GOROOT="/home/wen/soft/go"
GOTMPDIR=""
GOTOOLDIR="/home/wen/soft/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build060836953=/tmp/go-build -gno-record-gcc-switches"

What did you do?

$ tree .
.
├── A
│   └── a.go
├── B
│   └── b.go
├── main.go
└── pkgflag

2 directories, 4 files

$ cat A/a.go 
package A

import (
	"flag"
	"fmt"
)

var A = flag.String("a", "", "")
var A2 string

func init() {
	flag.StringVar(&A2, "a2", "", "")
	flag.Parse()
	fmt.Printf("pkg a, A=%v, A2=%v\n", *A, A2)
}

$ cat B/b.go 
package B

import (
	"flag"
	"fmt"
)

var B = flag.String("b", "", "")
var B2 string

func init() {
	flag.StringVar(&B2, "b2", "", "")
	flag.Parse()
	fmt.Printf("pkg b, B=%v, B2=%v\n", *B, B2)
}


$ cat main.go 
package main

import (
	"flag"
	"fmt"
	"wen/pkgflag/A"
	"wen/pkgflag/B"
)

func main() {
	flag.Parse()

	fmt.Printf("pkg main, A=%v,A2=%v, B=%v,B2=%v\n", *A.A, A.A2, *B.B, B.B2)
}

What did you expect to see?

./pkgflag -b=hello is working, package B and package main can get the value hello of B.B

What did you see instead?

$ ./pkgflag -b=hello
flag provided but not defined: -b
Usage of ./pkgflag:
  -a string
    
  -a2 string
    
$ ./pkgflag -h
Usage of ./pkgflag:
  -a string
    
  -a2 string
    
@beoran
Copy link

beoran commented May 8, 2019

I think you should not call flag.Parse() in the init() functions, but in the main() function of your application.

@robpike
Copy link
Contributor

robpike commented May 8, 2019

You can call flag.Parse in init if you like, although it's not a good idea for a number of reasons. But the important point is that your program should call it only once. And the best place is at the start of your main function.

@robpike robpike closed this as completed May 8, 2019
@chinglinwen
Copy link
Author

I remove the flag.Parse from package init, only main package do flag.Parse, it's just working.

$ ./pkgflag -b=aaa
pkg a, A=, A2=
pkg b, B=aaa, B2=
pkg main, A=,A2=, B=aaa,B2=

my bad, I've read some article that it can parse multiple times.

what I want is flag setting stick with package, so no need init variable(setting default secret etc) from main package.

It's working as expected ( without flag.parse in the imported package )

Thanks @beoran @robpike

@golang golang locked and limited conversation to collaborators May 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants