2 回答
TA贡献1818条经验 获得超3个赞
因为它们创建后需要赋值。动作顺序是:
创建变量
var n = flag.Bool("n", false, "omit trailing newline")
现在值为 false。用 赋值
flag.Parse()
。现在为变量分配了作为命令行参数传递的值。
TA贡献1942条经验 获得超3个赞
如果您检查此处的代码,您将看到有一个名为 的导出变量CommandLine
,它是一个指向FlagSet
. 这就是奇迹发生的地方。当您导入该库时,它就会被实例化。例如,当您调用导出函数时flag.Bool()
,该函数会依次调用方法 Bool()
,该方法有一个指向...的指针接收器FlagSet
。它将创建一个新的bool
来存储标志的值,调用以存储指向数据结构中BoolVar()
新创建的变量的指针(您需要跟踪以了解这是如何完成的),然后将完全相同的指针返回给您,以便您稍后可以获取当前的bool
FlagSet
BoolVar
值(可以是默认值,也可以是调用的结果的全新值Parse()
)
// CommandLine is the default set of command-line flags, parsed from os.Args.
// The top-level functions such as BoolVar, Arg, and so on are wrappers for the
// methods of CommandLine.
var CommandLine = NewFlagSet(os.Args[0], ExitOnError)
// NewFlagSet returns a new, empty flag set with the specified name and
// error handling property. If the name is not empty, it will be printed
// in the default usage message and in error messages.
func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
f := &FlagSet{
name: name,
errorHandling: errorHandling,
}
f.Usage = f.defaultUsage
return f
}
// A FlagSet represents a set of defined flags. The zero value of a FlagSet
// has no name and has ContinueOnError error handling.
//
// Flag names must be unique within a FlagSet. An attempt to define a flag whose
// name is already in use will cause a panic.
type FlagSet struct {
// Usage is the function called when an error occurs while parsing flags.
// The field is a function (not a method) that may be changed to point to
// a custom error handler. What happens after Usage is called depends
// on the ErrorHandling setting; for the command line, this defaults
// to ExitOnError, which exits the program after calling Usage.
Usage func()
name string
parsed bool
actual map[string]*Flag
formal map[string]*Flag
args []string // arguments after flags
errorHandling ErrorHandling
output io.Writer // nil means stderr; use Output() accessor
}
// Bool defines a bool flag with specified name, default value, and usage string.
// The return value is the address of a bool variable that stores the value of the flag.
func (f *FlagSet) Bool(name string, value bool, usage string) *bool {
p := new(bool)
f.BoolVar(p, name, value, usage)
return p
}
// Bool defines a bool flag with specified name, default value, and usage string.
// The return value is the address of a bool variable that stores the value of the flag.
func Bool(name string, value bool, usage string) *bool {
return CommandLine.Bool(name, value, usage)
}
回到你的问题:
为什么变量 n 和 sep 是指向标志变量的指针,而不是普通变量类型。
这是因为Parse()
可以操纵原始变量和新变量n
,并且sep
只会捕获原始值的副本。通过使用指针,您和其他人FlagSet
正在查看完全相同的变量。
- 2 回答
- 0 关注
- 101 浏览
添加回答
举报