import "github.com/spf13/pflag" var myint uint flagset := pflag.NewFlagSet("myapp", pflag.ExitOnError) flagset.UintVar(&myint, "myint", 12345, "an optional unsigned integer flag") // then, parse the command-line arguments flagset.Parse(os.Args)
import "github.com/spf13/pflag" var myints []int flagset := pflag.NewFlagSet("myapp", pflag.ExitOnError) flagset.IntSliceVar(&myints, "myints", []int{1,2,3}, "an optional slice of integers flag") // then, parse the command-line arguments flagset.Parse(os.Args)Here, we define a slice of integers flag named "myints", with a default value of {1,2,3}. When the user runs the program and provides a list of integers after the flag, such as `-myints 4 5 6`, it will be stored in the myints variable as [4,5,6]. In summary, the pflag library provides a convenient way to parse command-line arguments for your Go applications, using a simple and flexible API. The FlagSet type allows you to define flags and bind them to variables of different types, making it easy to configure your program from the command line.