import "github.com/spf13/pflag" var myFlag = pflag.String("myflag", "default_value", "help message") pflag.Parse() if pflag.Changed("myflag") { fmt.Printf("New value of myflag: %s\n", *myFlag) }
import "github.com/spf13/pflag" fs := pflag.NewFlagSet("myapp", pflag.ExitOnError) var myFlag = fs.String("myflag", "default_value", "help message") fs.Parse(os.Args[1:]) if fs.Changed("myflag") { fmt.Printf("New value of myflag: %s\n", *myFlag) }Description: This code creates a new FlagSet object with the name "myapp" and a setting to exit the program with an error if any flags are unrecognized or have errors. It then defines a flag named "myflag" with a default value of "default_value" and a help message. It parses command-line arguments starting from the second argument (the first being the name of the executable) using the FlagSet.Parse() function. Finally, it checks if the value of "myflag" has changed using the FlagSet.Changed() function, and prints the new value if it has.