flagset := pflag.NewFlagSet("example", pflag.ExitOnError) var strVal string flagset.StringVar(&strVal, "string", "default", "a string flag") // Parse command line args and print the value of strVal flagset.Parse(os.Args) fmt.Println("string flag value:", strVal)
flagset := pflag.NewFlagSet("example", pflag.ExitOnError) var strSliceVal []string flagset.StringSliceVar(&strSliceVal, "strings", []string{"default"}, "a string slice flag") // Parse command line args and print the value of strSliceVal flagset.Parse(os.Args) fmt.Println("string slice flag values:", strSliceVal)In this example, we create a new FlagSet and define a `strSliceVal` variable as a slice of strings. We set it as a string slice flag with the name "strings", default value `[]string{"default"}`, and usage message "a string slice flag". Then we parse the command line arguments with `flagset.Parse` and print the value of `strSliceVal`. Overall, the pflag package provides an efficient way to create and manage command line flags and subcommands in Go programs.