On this page:

https://pkg.go.dev/flag

There is an example:

var flagvar int func init() { flag.IntVar(&flagvar, "flagname", 1234, "help 
message for flagname") }

I understand that this is only an example, but there is no real need to
use an init() function when using the flag package.

When I first read this doc, I thought that using an init() function
was a requirement for the flag package. Other people may share the
same misunderstanding, because 2 years ago I ran into this usage pattern
in a third party library, and ran into problems when trying to compile the
library with a newer Go tool chain.

I mentioned that problem on this list:
https://groups.google.com/g/golang-nuts/c/rkCdS1EQwSM/m/vi44sM8vAwAJ

Would it be ok if I submit a patch to the docs to remove the init()
example from https://pkg.go.dev/flag ?

If people use the flag package from inside an init() function and it works 
for them,
that won’t change, since I am not proposing a code change.
Removing init() from the example may reduce confusion for other people
who might have the same confusion that I did.

—
Craig

-- 
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/ab5da20b-dee0-4c51-a434-32fd6708f7d9n%40googlegroups.com.

Reply via email to