This is a creation in Article, where the information may have evolved or changed.
2013-12-08 WCDJ
The Go language handles command-line arguments by using the flag package in the standard library.
Package flag implements command-line flag parsing.
http://golang.org/pkg/flag/
http://golang.org/pkg/
A few things to note:
1, through flag. String (), Bool (), Int () to define the flag to be used in the command line.
2, after the flag is defined, by calling flag. Parse () to parse the command-line arguments.
3, the format of the command-line arguments can be:
-flag xxx (using spaces, one-symbol)
--flag xxx (using spaces, two-symbols)
-FLAG=XXX (use equal sign, one-symbol)
--FLAG=XXX (use equal sign, two-symbol)
Where the Boolean parameter prevents the two semantics of parsing, it should be specified using the equals sign.
Test Case:
Package Mainimport ( "flag" "FMT") var input_pstrname = flag. String ("Name", "Gerry", "Input ur name") var input_piage = flag. Int ("Age", and "input ur Age") var input_flagvar intfunc Init () { flag. Intvar (&input_flagvar, "FlagName", 1234, "help message for FlagName")}func main () { init () & nbsp flag. Parse () //after parsing, the arguments after the flag is available as the slice flag. Args () or individually as flag. ARG (i). The arguments is indexed from 0 through flag. Narg () -1 //Args returns the Non-flag command-line arguments //NArg is the number of Arguments remaining after flags has been processed fmt. Printf ("args=%s, num=%d\n", flag.) Args (), flag. Narg ()) for I: = 0; I! = flag. Narg (); i++ { fmt. Printf ("arg[%d]=%s\n", I, flag. ARG (i)) } fmt. PRintln ("Name=", *input_pstrname) fmt. Println ("age=", *input_piage) fmt. Println ("Flagname=", Input_flagvar)}/*output:mba:filter gerryyang$./test_flag--name "AAA"-age=123-flagname=0x11 Para1 para2 para3args=[para1 para2 para3], num=3arg[0]=para1arg[1]=para2arg[2]=para3name= aaaage= 123flagname= 17*/
Reference:
Standard library-command line parameter parsing flag