package main import ( "flag" "fmt" ) func main() { var size int fs := flag.NewFlagSet("flags", flag.ExitOnError) fs.IntVar(&size, "size", 10, "the size of the array") fs.Parse([]string{"--size=20"}) if f := fs.Lookup("size"); f != nil { fmt.Printf("The size of the array is %d\n", f.Value.(flag.Getter).Get()) } }
The size of the array is 20
package main import ( "flag" "fmt" ) func main() { var ( name string age int ) fs := flag.NewFlagSet("flags", flag.ExitOnError) fs.StringVar(&name, "name", "", "the name of the person") fs.IntVar(&age, "age", 0, "the age of the person") fs.Parse([]string{"--name=john"}) if f := fs.Lookup("age"); f != nil { fmt.Println("Age flag is present") } else { fmt.Println("Age flag is not present") } }
Age flag is not presentPackage library: "flag" package in Go language.