Home >Backend Development >Golang >How to use command line arguments in Go?
In the Go language, command line parameters are a very important way to pass input to the program and specify runtime behavior. Go provides a standard library flag
to parse command line parameters. This article will introduce how to use command line parameters in Go.
Command line parameters are parameters passed to the program through the command line when the program is running, and are used to specify the behavior and input of the program when it is running. For example, the ls
command in Linux can accept multiple command line parameters, such as -l
to list detailed information, -a
to show and hide Documents etc.
In Go, we can use os.Args
to get all the command line arguments passed when the program is running. os.Args
is a string slice, the first element is the name of the program, and the following elements are the command line parameters. For example:
package main import ( "fmt" "os" ) func main() { fmt.Println(os.Args) }
Run the program and pass in the two parameters hello
and world
:
$ go run main.go hello world
Output result:
[/tmp/go-build3065006307/b001/exe/main hello world]
As you can see, os.Args
contains the name of the program and two command line parameters.
Although we can obtain all command line parameters through os.Args
, in actual development, we usually use more Flexible way to parse command line arguments. The Go standard library provides the flag
package to parse command line parameters.
flag
The package provides three types of command line parameters: boolean, string and integer. These three parameters can be defined respectively through flag.Bool()
, flag.String()
and flag.Int()
.
The following is a simple example that defines a Boolean and a string command line parameter:
package main import ( "flag" "fmt" ) func main() { // 定义命令行参数 var ( isVerbose bool name string ) flag.BoolVar(&isVerbose, "v", false, "verbose") flag.StringVar(&name, "name", "", "name of the user") // 解析命令行参数 flag.Parse() // 输出命令行参数 fmt.Printf("isVerbose=%v, name=%s ", isVerbose, name) }
In the above example, we use flag.BoolVar()
and flag.StringVar()
define a Boolean and a string command line parameter respectively. These two functions have a total of four parameters:
After defining the command line parameters, you need to call the flag.Parse()
function to parse the command line parameters.
Run the program and pass in the two command line parameters -v
and -name="Alice"
:
$ go run main.go -v -name=Alice
Output results:
isVerbose=true, name=Alice
We can see that the flag
package can easily parse command line parameters and store the values into corresponding variables.
In addition to using the three types of command line parameters provided by the flag
package, we can also customize some types of command line parameters.
For example, we can define a drop-down box type command line parameter to let the user choose one from several options:
package main import ( "fmt" "github.com/AlecAivazis/survey/v2" ) func main() { // 定义下拉框选项 options := []string{"red", "blue", "green"} // 定义命令行参数 var color string prompt := &survey.Select{ Message: "Pick a color:", Options: options, } survey.AskOne(prompt, &color) // 输出命令行参数 fmt.Printf("color=%s ", color) }
In the above example, we use a third-party librarygithub.com/AlecAivazis/survey/v2
Defines a drop-down box option. Then, use the survey.AskOne()
function to display the drop-down box and let the user select.
Run the program:
$ go run main.go
Output result:
? Pick a color: ▸ red blue green Answer: red color=red
We can see that the user can select a color from several options and store the selected value in color
variable.
This article introduces how to use command line parameters in Go. We can use os.Args
to get all command line parameters, or we can use the flag
package or a custom method to parse command line parameters. Using command line parameters can make our programs more flexible and easier to use, and is also a necessary skill for writing high-quality command line tools.
The above is the detailed content of How to use command line arguments in Go?. For more information, please follow other related articles on the PHP Chinese website!