flag--command Line parameters define multi-label example

Source: Internet
Author: User

//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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.