好吧,我正在掌握眼镜蛇的窍门。 为了识别正在调用子子模块的父级,我必须使用指针,因为有多个父级(以及多个祖父母)。
现在分配本地标志,下面的代码正确吗?
var (
GcpAccessAnnotateCmd = *AnnotateCmd
GcpApprovalsAnnotateCmd = *AnnotateCmd
SshAccessAnnotateCmd = *AnnotateCmd
SshApprovalsAnnotateCmd = *AnnotateCmd
)
func init() {
GcpAccessAnnotateCmd.Flags().
StringP("note", "n", "", "Note/Comment to add to the request or task")
GcpApprovalsAnnotateCmd.Flags().
StringP("note", "n", "", "Note/Comment to add to the request or task")
SshAccessAnnotateCmd.Flags().
StringP("note", "n", "", "Note/Comment to add to the request or task")
SshApprovalsAnnotateCmd.Flags().
StringP("note", "n", "", "Note/Comment to add to the request or task")
}
换句话说,既然我有 4 个指针,我是否必须声明该标志 4 次?
您还可以对父命令使用持久标志。否则,是的,您必须将标志分配给使用它的每个命令。不过,您可以使用循环而不是重复同一行。