« Back to Index

[Golang Long and Short Flags]

View original Gist on GitHub

Tags: #go #golang #flags

Long and Short Flag.go

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