"Blockchain + Era will undoubtedly be the next tuyere, but now the blockchain industry professionals are experiencing bottlenecks," brother Lian Education blockchain college Dean Yin Cheng said, "I hope that through the brother even education blockchain training College for the community to train and transport more high-quality blockchain technology talents."
When deploying Golang projects, it is unavoidable to set some parameters through the command line, so how do you manipulate command-line arguments in Golang? You can use the flag library and the OS Library.
Use of 1.flag libraries
The Go Language standard library provides a flag package for fast resolution of command-line parameters, with the following steps in general:
A. by flag.string (), flag. Bool (), flag. Int () to define the parameters that need to be used in the command line.
B. After the flag is defined, the command-line arguments are parsed by calling Flag.parse ().
C. Get Flag.string (), flag. Bool (), flag. The return value of the method, such as Int (), which corresponds to the parameter entered by the user.
It is important to note that flag. The value returned by XXX () is the memory address of the variable, which is obtained by adding * (asterisk) before the variable to get the value.
Description
Like flag. INT, Flag. Bool, Flag. The format of a function such as String is the same, the call requires the passing of 3 parameters
The parameters are described as follows:
The ARG represents the parameter name, which is provided to the user when it is in the console.
Second, ARG represents the default value, which is used if the user does not assign a value to the parameter in the console.
C. ARG indicates the usage description and description, which is displayed when you enter-ARG in the console, similar to-help
See a complete example of use:
Packagemain
Import
"Flag"
"FMT"
)
Funcmain () {
Married:=flag. Bool ("Married", False, "areyoumarried?")
Age:=flag. Int ("Age", "howoldareyou")
Name:=flag. String ("name", "", "Whatyourname?")
Varaddressstring
Flag. The first parameter of a function such as Stringvar is replaced with the variable address, followed by the parameter and flag. string is the same.
Flag. Stringvar (&address, "address", "GuangZhou", "Whereisyouraddress?")
Flag. Parse ()//parse the input parameters
Fmt. PRINTLN ("Output parameter married value is:", *married)//Not add *, the output is memory address
Fmt. Println ("The value of the output parameter age is:", *age)
Fmt. Println ("The value of the output parameter name is:", *name)
Fmt. Println ("The value of the output parameter address is:", address)
}
As can be seen from the results of the operation, the address parameter we do not specify a value, the output is the default value.
Also, the = number behind the-arg is not required and can be replaced with a space.
If you want to see the use of all the parameters of the program, you can use-help to view it as follows:
Use of 2.os libraries
See examples directly
Packagemain
Import
"OS"
"FMT"
)
Funcmain () {
Args:=os. args//get all parameters entered by the user
ifargs==nil| | Len (args) <2{
Usage ()//If the user does not have input, or if the number of arguments is insufficient, call the function to prompt the user
Return
}
name:=args[1]//gets the first parameter of the input
age:=args[2]//gets the second parameter of the input
Fmt. Println ("Yournameis:", Name, "\nyourageis:", age)
}
Varusage=func () {
Fmt. Println ("Youname?");
Fmt. Println ("Youage?");
}
Personal feeling or flag. Getting the parameters is a bit easier because flag is more relevant to parameter usage and can be viewed in detail.
Finally, how to get the memory address and receive method of the variable
Packagemain
Import
"FMT"
)
Funcmain () {
varnamestring= "ABC"
varnameaddress*string=&name;//if you want to receive the memory address of the variable, add the * number before the variable corresponds to the type
Fmt. PRINTLN ("Name Variable memory address:", &name)
Fmt. Println ("Nameaddress value:", nameaddress)
Fmt. Println ("&name==nameaddress?", &name==nameaddress)
Fmt. Println ("nameaddress value of the corresponding variable:", *nameaddress)///memory address before the plus * can see the corresponding value.
Fmt. PRINTLN ("Name variable value:", *&*&name)//* number and & can be used multiple times, reading from right to left
}
High-energy early warning, brother even education block chain live course August continues to hit the hot!
The original price of 1188 Yuan of 12 block chain advanced courses, now only need 1 Yuan!
Also can receive free "Go Language foundation actual combat project development" and "Go language Advanced Combat Project development" textbook Two!!
Limited time limit!! First come first served!!
Http://www.ydma.cn/open/course/24