import ( "flag" "fmt" ) func main() { fs := flag.NewFlagSet("myProgram", flag.ExitOnError) var myString string fs.StringVar(&myString, "str", "", "a string flag") fs.Parse([]string{"--str=hello"}) fmt.Println(myString) }
import ( "flag" "fmt" ) func main() { fs := flag.NewFlagSet("myProgram", flag.ExitOnError) var myString string fs.StringVar(&myString, "str", "", "a string flag") fs.Parse([]string{}) fmt.Println(myString) }In this example, we create a new FlagSet and add a string flag "str". We then parse the flag using the Parse function without specifying a value for "str". Since we did not provide a value for "str", myString will be an empty string. Overall, the go flag package library provides a way to parse command-line arguments in a structured and easy-to-use way. The FlagSet StringVar function is just one function in this library that can be used to specify different types of flags for a program.