import ( "flag" "fmt" ) func main() { fs := flag.NewFlagSet("myapp", flag.ExitOnError) var num int fs.IntVar(&num, "num", 0, "a number") fs.Parse([]string{"--num", "42"}) fmt.Println(num) }
package main import ( "fmt" "flag" ) func main() { fs := flag.NewFlagSet("myapp", flag.ExitOnError) var num int fs.IntVar(&num, "n", 0, "a number") fs.Parse([]string{"-n", "123"}) fmt.Println(num) }In this example, we create a `FlagSet` called "myapp". We define an integer flag called "n" with a default value of 0 and a short description. We then parse a command line argument using `fs.Parse` and print the value of `num`, which is 123. Package library: `flag`