Go language - get command line arguments

Go language - get command line arguments

1, flag library

Go language standard library provides a package for an alert flag to parse the command line parameters, using substantially the following steps

a, defined by the command line parameters to use by flag.String (), flag.Bool (), flag.Int () or the like.

b, have been defined in the flag, to parse the command line parameters by calling flag.Parse ().

C, obtaining flag.String (), returns the value flag.Bool (), flag.Int () method and the like, i.e., corresponding to the user input parameters.

Note that flag.Xxx () return value is the memory address of a variable, when you want to get the value to be obtained by adding before the variable * (asterisk).

Description:

flag.Int, flag.Bool, flag.String format such functions are the same, when the call need to pass three parameters

Parameters as follows:

The first arg parameter name represents, at the time of the console, available to users.

The second arg indicates the default value if the user at the console useless to the parameter assignment, it would use the default value.

The third indication arg illustrated and described, -arg input in the console displays this description when similar -help

See a complete example of the use

main Package Penalty for 

Import ( 
	"Flag" 
	"fmt" 
) 

FUNC main () { 
	Married: = flag.Bool ( "? Married", false, "Are you Married") 
	Age: = flag.Int ( "Age", 22, " Old are you How ")? 
	name: = flag.String (" name "," "," the What your name? ") 
	var address String 
	this function //flag.StringVar first argument into the variable address behind parameters and flag.String is the same. 
	flag.StringVar (& address, "address", "GuangZhou", "the Where IS your address?") 
	flag.Parse () // parse input parameter 
	fmt.Println ( "value of the output parameter married is:", * married ) // without an asterisk, then the output of the memory address 
	fmt.Println ( "value of the output parameter age is:", * age) 
	fmt.Println ( "the output value of the parameter name is:", * name) 
	fmt.

 operation result:

The output values of the parameters are married: false
value of the output parameter is age: 22
parameter name is output:
the value of the parameter is an output address: GuangZhou

As can be seen from the junction run, we did not address the parameter value is specified, the default value is output

Further, -arg = back number is not essential space may be used instead.

If you want to view the program all parameters can be used to view -help

2, using the os library

Direct look at an example

main Package 

Import ( 
	"FMT" 
	"OS" 
) 

FUNC main () { 
	args: = os.Args // get all the parameters entered by the user 
	IF args == nil || len (args) <2 { 
		the Usage () // if the user input, or the number of parameters is insufficient, prompts the user to call the function 
		return 
	} 
	name: args = [. 1]; 
	Age: = args [2] 
	fmt.Println ( "iS your name:", name, "\ nyour IS Age: ", Age) 

} 
var = FUNC the Usage () { 
	fmt.Println (" you name ")? 
	fmt.Println (" you Age ")? 
}

 operation result:

you name?
you age?

Personal feeling flag acquisition parameters useful, because more appropriate flag usage parameters, and you can view the details.

Last added

main Package 

Import "FMT" 

FUNC main () { 
	var String name = "ABC" 
	var = & nameAddress * String name // To accept the memory address of the variable, the type of a variable corresponding to the front with * 
	fmt.Println ( "name variable storage address: ", & name) 
	fmt.Println (" nameAddress values: ", nameAddress) 
	? fmt.Println (" & name == nameAddress ", & name == nameAddress) 
	fmt.Println (" nameAddress corresponding to the value of the variable: " , * nameAddress) 
	// memory address plus * good front you can see the value corresponding to the 
	fmt.Println ( "name value of the variable:", * & * & name ) // * and ampersands may repeatedly be used in combination, when reading from right to left 
}

operation result:

memory address of variable name: 0xc0000321f0
nameAddress value: 0xc0000321f0
& name == nameAddress to true?
nameAddress values corresponding variables: abc
value of the name variable: abc

-------------------------------------------------------------------------

[theory]

& Meanings of the symbols are taken to address the variables, such as: the address of a variable is a & 

The symbol * means that pointer values, such as: * & a, where the value of the address is a variable, of course, is the value of a

[Simple explanation]

* & Balance out and, bearing in mind, can offset * &, but is not & canceling a * and a & * is the same, the values ​​are a value of 1 (since & cancel each other out *)

Similarly, a and * & * & * & * & a is the same, is 1 (because 4 * & cancel each other out)

 

 

 

 




Guess you like

Origin www.cnblogs.com/chenjunjin/p/12180627.html