// Define some flags var fooFlag = pflag.String("foo", "default", "a string flag") var barFlag = pflag.Int("bar", 42, "an integer flag") // Parse the command line arguments pflag.Parse() // Look up the value of the "foo" flag fooVal := pflag.Lookup("foo").Value.String() // Look up the value of the "bar" flag barVal := pflag.Lookup("bar").Value.String() // Print out the flag values fmt.Printf("foo=%v bar=%v\n", fooVal, barVal)
// Define a flag var someFlag = pflag.Bool("some-flag", false, "a boolean flag") // Parse the command line arguments pflag.Parse() // Check if the flag was set if pflag.Lookup("some-flag").Value.String() == "true" { fmt.Println("The some-flag flag was set!") }In this example, we define a boolean flag, parse the command line arguments, and then use FlagSet Lookup to check whether the flag was set. Overall, the pflag package provides a powerful and flexible way to parse and handle command-line arguments in Go. The FlagSet Lookup method adds even more flexibility to this package by allowing you to programmatically access and manipulate flag values.