Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Golang Long and Short Flags] #go #golang #flags
var myFlagType string
func init() {
const (
flagValue = "default value is foo"
flagUsage = "this is my flag explanation"
)
flag.StringVar(&myFlagType, "foo", flagValue, flagUsage)
flag.StringVar(&myFlagType, "f", flagValue, flagUsage+" (shorthand)")
flag.Parse()
}
// this works by using an alternative flag syntax, which allows you to
// specify a 'variable' to assign the incoming flag value to.
// this is different to `flag.String` where the returned type is a pointer.
//
// in the above example we can see we specify --foo and -f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment