Variables 在go中声明没有值的全局变量

Variables 在go中声明没有值的全局变量,variables,go,global,Variables,Go,Global,我有一个程序需要1个或2个参数,这取决于用户想要运行什么 var ( clientSet = tools.NewClientSet(os.Args[2]) ) func main { if os.Args[1] == "validate" { // run validate function, no need for user to have os.Args[2] }else if os.Args[1] == "sync" { // run syn

我有一个程序需要1个或2个参数,这取决于用户想要运行什么

var (
   clientSet = tools.NewClientSet(os.Args[2])
)
func main {
    if os.Args[1] == "validate" {
       // run validate function, no need for user to have os.Args[2]
    }else if os.Args[1] == "sync" {
      // run sync function that requires os.Args[2]
    }
}
func foo{
   tools.Manage(clientSet)
}
我需要
clientSet
变量是全局变量,但是如果用户只想使用validate函数,我不需要用户有os.Args[2]。将
clientSet
函数放入
main()
将使我的
foo()
函数被破坏,并且我不能用空值声明变量

因此,我希望我的用户能够顺利运行
go-run-main.go-validate
go-run-main.go-sync-production

*生产是一个任意值


我可以让我的用户运行
go-run-main.go-validate
来解决这个问题,但那是不雅观的。解决这个问题的最佳方法是什么

答案通常是不要使用globals。相反,让
foo
获取一个参数
foo(clientSet-clientSet)
,并仅在需要时实例化它。

答案通常是不要使用全局变量。相反,让
foo
获取一个参数
foo(clientSet-clientSet)
,并仅在需要时实例化它。

在这种情况下,我甚至不认为需要全局变量。您只需让同步功能接受
ClientSet
,例如
func-sync(c-ClientSet)
。但是如果您真的需要全局变量,那么您不应该这样做,除非您希望您的程序在没有参数存在时死机

var (
   clientSet = tools.NewClientSet(os.Args[2])
)
您应该做的是为它指定一个默认值或类型的零值

var (
   clientSet tools.ClientSet
)
您的主要功能如下所示:

var (
    clientSet tools.ClientSet
)

func main() {

    if len(os.Args) < 2 {
        os.Exit(1)
    }

    switch os.Args[1] {
    case "validate":
        validate()

    case "sync":

        if len(os.Args) < 3 {
            os.Exit(1)
        }

        clientSet = tools.NewClientSet(os.Args[2])
        sync()
    default:
        // place your default case here
    }

}
var(
clientSet工具.clientSet
)
func main(){
如果len(os.Args)<2{
操作系统退出(1)
}
开关操作系统参数[1]{
案例“验证”:
验证()
案例“同步”:
如果len(os.Args)<3{
操作系统退出(1)
}
clientSet=tools.NewClientSet(os.Args[2])
sync()
违约:
//将默认案例放在此处
}
}

尽管如此,我还是建议您将
ClientSet
传递给sync函数,因为它将避免全局变量。

在这种情况下,我甚至不认为需要全局变量。您只需让同步功能接受
ClientSet
,例如
func-sync(c-ClientSet)
。但是如果您真的需要全局变量,那么您不应该这样做,除非您希望您的程序在没有参数存在时死机

var (
   clientSet = tools.NewClientSet(os.Args[2])
)
您应该做的是为它指定一个默认值或类型的零值

var (
   clientSet tools.ClientSet
)
您的主要功能如下所示:

var (
    clientSet tools.ClientSet
)

func main() {

    if len(os.Args) < 2 {
        os.Exit(1)
    }

    switch os.Args[1] {
    case "validate":
        validate()

    case "sync":

        if len(os.Args) < 3 {
            os.Exit(1)
        }

        clientSet = tools.NewClientSet(os.Args[2])
        sync()
    default:
        // place your default case here
    }

}
var(
clientSet工具.clientSet
)
func main(){
如果len(os.Args)<2{
操作系统退出(1)
}
开关操作系统参数[1]{
案例“验证”:
验证()
案例“同步”:
如果len(os.Args)<3{
操作系统退出(1)
}
clientSet=tools.NewClientSet(os.Args[2])
sync()
违约:
//将默认案例放在此处
}
}
尽管如此,我还是建议您将
ClientSet
传递给sync函数,因为它将避免全局变量。

只需使用len(os.Args)函数即可

var (
    clientSet tools.ClientSet
)

func main() {
    if len(os.Agrs) == 1 {
        // just the file name
    } else if len(os.Args) == 2 {
        if os.Args[1] == "validate" {
            // run validate function, no need for user to have os.Args[2]
        } else if os.Args[1] == "sync" {
            // sync with no argument show error
        }
    } else if len(os.Args) == 3 {
        if os.Args[1] == "validate" {
            clientSet = tools.NewClientSet(os.Args[2])
        } else {
            // non validate with the second arg
        }
    } else {
        // else, if required
    }
}
尽管我建议你不要使用全局变量。尽可能避免。

只需使用len(os.Args)函数即可

var (
    clientSet tools.ClientSet
)

func main() {
    if len(os.Agrs) == 1 {
        // just the file name
    } else if len(os.Args) == 2 {
        if os.Args[1] == "validate" {
            // run validate function, no need for user to have os.Args[2]
        } else if os.Args[1] == "sync" {
            // sync with no argument show error
        }
    } else if len(os.Args) == 3 {
        if os.Args[1] == "validate" {
            clientSet = tools.NewClientSet(os.Args[2])
        } else {
            // non validate with the second arg
        }
    } else {
        // else, if required
    }
}
尽管我建议你不要使用全局变量。尽可能避免