//TestFlag2 project Main.gopackage mainimport ("Flag" "FMT") func main () {//Definition flag/ /Way one: through flag. A flag such as String (), Bool (), Int (). The Xxx () method, which returns a corresponding pointer to nameptr: = flag. String ("name", "Anson", "User ' name") Ageptr: = flag. Int ("Age", Vipptr, "user's age"): = flag. Bool ("VIP", True, "is a VIP user")//Way two: through flag. The Xxxvar () method binds the flag to a variable, which returns a value of type var email stringflag. Stringvar (&email, "email", "[email protected]", "User's email")//There is a third way through flag. Var () binds a custom type, and the custom type needs to implement the value interface (receives must be a pointer)//flag. Var (&flagval, "name", "Help message for FlagName")//Parse command-line arguments, and values are saved to the defined Flagflag. Parse ()//After parsing parse, you can directly use the flag itself (pointer type) or the bound variable (value type)//and flag. Args (), flag. ARG (i) to get non-flag command line arguments others: = flag. Args ()//Save flags other than the variable fmt.println ("Name:", *nameptr) fmt. Println ("Age:", *ageptr) fmt. Println ("VIP:", *vipptr) fmt. Println ("Email:", email) fmt. Println ("Other:", others) fmt. Println ("---------") for I: = 0; I < Len (flag). Args ()); i++ {fmt. Println ("Arg", I, "=", flag.) ARG (i))}}
Execution Result:
Testflag2 -name aa-age 33-vip=false AAA BBB CCC 111 222name:aaage:33vip:falseemail: [email protected]other: [AAA BBB CCC 111 222]---------ARG 0 = aaaarg 1 = bbbarg 2 = Cccarg 3 = 111Arg 4 = 222
flag--command Line parameters define multi-label example