
为结构体字段赋值时。
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 允许您隔离产生值的逻辑片段,为变量创建一个不会污染周围命名空间的作用域环境。
匿名函数中使用的变量(在本例中为 tailscale/tailscale)不会转义到周围的代码中。这使得代码更容易推理,因为变量仅在需要时才存在。
虽然 cmd/tailscale/cli/configure.go#var configureCmd = &ffcli.Command{} 相当于 fs, some第二种形式的优点可能是:
FlagSet: newFlagSet("configure") 是 true,但 的修改需要更复杂的操作或计算,这些更改可以轻松地合并到匿名函数中,而无需更改 FlagSet: (func() *flag.FlagSet { fs := newFlagSet("configure"); return fs})() 的结构。看看 tailscale 代码,那个特定的 IIFE使用似乎仅限于该一个实例。
以上就是为什么立即调用内联函数,而不是仅仅调用其包含的函数?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号