为结构体字段赋值时。
FlagSet: (func() *flag.FlagSet {
fs := newFlagSet("configure")
return fs
})(),
我觉得就相当于调用newFlagSet("configure")。这样写有什么好处。
阅读源码时的问题。需要知道他为什么这样写。
正确答案
快速搜索,此代码来自tailscale/tailscale
, cmd/tailscale/cli/configure.go#var configureCmd = &ffcli.Command{}
var configureCmd = &ffcli.Command{
Name: "configure",
ShortHelp: "[ALPHA] Configure the host to enable more Tailscale features",
LongHelp: strings.TrimSpace(`
The 'configure' set of commands are intended to provide a way to enable different
services on the host to use Tailscale in more ways.
`),
FlagSet: (func() *flag.FlagSet {
fs := newFlagSet("configure")
return fs
})(),
Subcommands: configureSubcommands(),
Exec: func(ctx context.Context, args []string) error {
return flag.ErrHelp
},
}
该代码使用函数文字(匿名函数),然后立即调用。
这称为立即调用函数表达式 (IIFE)。这在 JavaScript 等语言中更常见,但在 Go 中也很有用。
在 Go 中,IIFE 允许您隔离产生值的逻辑片段,为变量创建一个不会污染周围命名空间的作用域环境。
匿名函数中使用的变量(在本例中为 fs
)不会转义到周围的代码中。这使得代码更容易推理,因为变量仅在需要时才存在。
虽然 FlagSet: newFlagSet("configure") 是 true,但
相当于 FlagSet: (func() *flag.FlagSet { fs := newFlagSet("configure"); return fs})()
, some第二种形式的优点可能是:
- 可扩展性:如果将来对
newFlagSet("configure")
的修改需要更复杂的操作或计算,这些更改可以轻松地合并到匿名函数中,而无需更改configureCmd
的结构。 - 调试:在调试会话期间可以轻松注释、记录或修改封装的逻辑,而不会干扰周围的代码。
看看 tailscale 代码,那个特定的 IIFE使用似乎仅限于该一个实例。