Go command line parameter analysis-built-in module flag study notes

import "flag"

The flag package implements the analysis of command line parameters. Each parameter is considered as a record, defined according to the actual situation, to a set collection. Each has its own status parameter.

The normal process when using flag: 

1. 通过flag.String(), flag.Bool(), flag.Int()等函数来定义命令行中需要使用的参数。

2. 在定义完flag后,通过调用flag.Parse()来进行对命令行参数的解析。

3. 获取flag.String(), flag.Bool(), flag.Int()等方法的返回值,即对应用户输入的参数.

 注意的是flag.Xxx()返回的值是变量的内存地址,要获取值时要通过在变量前加*(星号)获取.

说明:
像flag.Int、flag.Bool、flag.String这样的函数格式都是一样的,调用的时候需要传入3个参数
参数的说明如下:
第一个arg表示参数名称,在控制台的时候,提供给用户使用.
第二个arg表示默认值,如果用户在控制台没有给该参数赋值的话,就会使用该默认值.
第三个arg表示使用说明和描述,在控制台中输入-arg的时候会显示该说明,类似-help

The registerable flag types are: Bool / Int / Int64 / Uint / Uint64 / Float / Float64 / String / Duration / Var

Guess you like

Origin blog.csdn.net/wuchenlhy/article/details/83105533