-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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
[bug report] flag provided but not defined: -test.v #46869
Comments
still hava this in 1.16.3 |
What is the exact command you ran? |
Sorry, you cannot call flag.Parse in init because it conflicts with the flags that are passed to the test binary. Move flag.parse to your main function |
What Dave said. This is expected and documented: https://golang.org/doc/go1.13#testing, #31859. Closing. |
I saw this question,but when will solve this problem.or should I notice that question in any program? |
go test -v -run + myfun |
@the-moment By the way, testing function will auto call flag.Parse(). Style below is ok package config
import "flag"
var mode string
func init() {
flag.StringVar(&mode, "mode", "local", "go run main.go -mode 'local'")
} package config
import (
"fmt"
"testing"
)
func TestDefault(t *testing.T) {
fmt.Println(mode)
}
Output hehe As it suggests, |
Thats about it. Because flag.Parse operates on global state it must only be called once. The problem is both your package and, when compiled as a test, the testing package both define and parse flags, there is no way to make it work with flag.Parse called from init. Either:
The root problem is the global flag.Parse singleton, but sadly that ship has long sailed. You can work around this by doing one or more of the following:
|
Cannot use `Flag.parse` in init: golang/go#46869 (comment)
Go 1.13.15 has a bug for package flag. Code below can help reproduce it.
config/init.go
config/init_test.go
output
go 1.12.17 ok
go 1.13.15
The text was updated successfully, but these errors were encountered: