1. Introduction
What happens when we want to start the Golang program from the command line and get the various formal parameters of the input?
2.os.Args
Args is a string slice used to store all command line arguments, including the go Run main.go part. The following is an example:
func main(a) {
for i ,v := range os.Args {
fmt.Println(i, v)
}
}
Copy the code
The command line starts with the following parameters: go run main. Go-name korbin aaa BBB CCC
0 C:\Users\Korbin\AppData\Local\Temp\go-build427584346\b001\exe\main.exe
1 -name
2 korbin
3 aaa
4 bbb
5 ccc
Copy the code
3. The flag bag
Golang’s built-in Flag package provides a series of functional interfaces for parsing command-line arguments. Using Flag includes the following steps:
3.1 Defining flag command line parameters
The first step is to define the command line parameters in the following three ways:
1. Use flag.string (), Bool(), Int() and other flag.xxx () methods, which return a pointer:
ip := flag.Int("name"."korbin"."the author's name")
Copy the code
There are three parameters in the method: name: indicates the name of the specified parameter. On the cli, enter -name or –name value: indicates the default value. Usage: Parameter Description Description
2. Bind parameter values to a variable using flag.xxxvar (). No return value is returned, for example:
var name string
flag.StringVar(&name , "name"."korbin"."the author's name")
Copy the code
The first argument to the method is the bound variable, passing a pointer; The subsequent parameters are the same as the preceding ones.
3. Bind the custom type with flag.var (). The custom type needs to implement the Value interface (which must be a pointer), for example:
flag.Var(&name, "name"."the author's name")
Copy the code
For this type of flag, the default value is the initial value of the variable type, that is, if the input is an int, it defaults to 0, string defaults to null, and so on.
3.2 flag. The Parse ()
Second, call flag.parse () to Parse the command line arguments to the defined flag:
flag.Parse()
Copy the code
The parsing function will stop when it encounters the first non-flag command line parameter, which does not meet the command line syntax. For example, if the command line parameter is CMD –flag=true ABC, the first non-flag command line parameter is ABC.
3.3 Using Command-line Parameters
Through the above two steps of defining parameters and parsing parameters, we can already get our parameters. For non-command line parameters, you can also obtain ~ by flag.args (), flag.arg (I)
The complete example is as follows:
package main
import (
"flag"
"fmt"
"os"
)
func main(a) {
s := flag.String("name"."korbin"."Here's the author's name.")
flag.Parse()
fmt.Println("Command line parameter name value:",*s)
fmt.Printf("Non-command line parameter slice: %v", flag.Args())
}
Copy the code
The command line starts with the following parameters: go run main. Go-name korbin aaa BBB CCC
Command line parameter name value: korbin Non-command line parameter slice: [aaa BBB CCC]Copy the code