Edit:
Actually revisiting your code it works! What version of Go are you using? Maybe you need to rebuild your code.
The decision of what Usage
function to call is in the flag.go
source file, line 708, unexported function usage()
(this is from Go 1.4):
func (f *FlagSet) usage() {
if f.Usage == nil {
if f == CommandLine {
Usage()
} else {
defaultUsage(f)
}
} else {
f.Usage()
}
}
This tells if the FlagSet.Usage
is not nil
, it will be called. If it is not called for you, it's most likely you're using a Go version prior to 1.4 (which you confirmed in your comment).
But since you're using the default flag.CommandLine
flagset, you can simply write:
// Note "flag.Usage" instead of "f.Usage"
flag.Usage = func() {
fmt.Println("foo bar")
}