package main import ( "fmt" "github.com/spf13/pflag" ) func main() { var names []string fs := pflag.NewFlagSet("example1", pflag.ExitOnError) fs.StringSliceVar(&names, "name", []string{}, "Names to greet") fs.Parse([]string{"--name", "Alice", "--name", "Bob"}) for _, name := range names { fmt.Printf("Hello, %s!\n", name) } }
package main import ( "fmt" "github.com/spf13/pflag" ) func main() { var fruits []string fs := pflag.NewFlagSet("example2", pflag.ExitOnError) fs.StringSliceVar(&fruits, "fruit", []string{"apple", "banana"}, "Fruits to eat") fs.Parse([]string{"--fruit=orange", "--fruit=grape"}) for _, fruit := range fruits { fmt.Printf("I like %s.\n", fruit) } }This code defines a `FlagSet` with a `StringSliceVar` flag named "fruit". The flag is initially set to a slice containing "apple" and "banana", and two additional fruits "orange" and "grape" are added using `fs.Parse`. The program then prints a message for each fruit.