为了账号安全,请及时绑定邮箱和手机立即绑定

为什么Go的包标志使用指针?

为什么Go的包标志使用指针?

Go
撒科打诨 2023-08-21 14:28:00
考虑以下代码gopl.io/ch2/echo4package mainimport (    "flag"    "fmt"    "strings")var n = flag.Bool("n", false, "omit trailing newline")var sep = flag.String("s", " ", "separator")func main() {    flag.Parse()    fmt.Print(strings.Join(flag.Args(), *sep))    if !*n {        fmt.Println()    }}我感兴趣的是为什么变量n和sep是指向标志变量的指针,而不是普通的变量类型。
查看完整描述

2 回答

?
泛舟湖上清波郎朗

TA贡献1818条经验 获得超3个赞

因为它们创建后需要赋值。动作顺序是:

  1. 创建变量var n = flag.Bool("n", false, "omit trailing newline")现在值为 false。

  2. 用 赋值flag.Parse()。现在为变量分配了作为命令行参数传递的值。


查看完整回答
反对 回复 2023-08-21
?
手掌心

TA贡献1942条经验 获得超3个赞

如果您检查此处的代码,您将看到有一个名为 的导出变量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正在查看完全相同的变量。


查看完整回答
反对 回复 2023-08-21
  • 2 回答
  • 0 关注
  • 71 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信