flag 包输出命令行参数的默认值的方法是通过调用 flag.PrintDefaults()
函数实现的。这将打印出所有已定义的 flag 的名称、默认值和描述信息。例如:
package main
import (
"flag"
"fmt"
)
func main() {
var name string
var age int
flag.StringVar(&name, "name", "John Doe", "the name to greet")
flag.IntVar(&age, "age", 30, "the age of the person to greet")
flag.Parse()
fmt.Println("Name:", name)
fmt.Println("Age:", age)
flag.PrintDefaults()
}
运行上面的代码将输出以下信息:
Name:
the name to greet (default "John Doe")
-age int
the age of the person to greet (default 30)