手掌心
如果您检查此处的代码,您将看到有一个名为 的导出变量CommandLine,它是一个指向FlagSet. 这就是奇迹发生的地方。当您导入该库时,它就会被实例化。例如,当您调用导出函数时flag.Bool(),该函数会依次调用方法 Bool(),该方法有一个指向...的指针接收器FlagSet。它将创建一个新的bool来存储标志的值,调用以存储指向数据结构中BoolVar()新创建的变量的指针(您需要跟踪以了解这是如何完成的),然后将完全相同的指针返回给您,以便您稍后可以获取当前的boolFlagSetBoolVar值(可以是默认值,也可以是调用的结果的全新值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正在查看完全相同的变量。