Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active January 3, 2024 19:35
Show Gist options
  • Save Integralist/74f55c0587238536f24644715e0f3325 to your computer and use it in GitHub Desktop.
Save Integralist/74f55c0587238536f24644715e0f3325 to your computer and use it in GitHub Desktop.
[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