duanming7833
duanming7833
2018-11-08 04:56
浏览 52
已采纳

声明没有值的全局变量

I have a program that requires either 1 or 2 arguments depending on what the users wants to run

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)
}

I need the clientSet variable to be global, but I dont need the users to have os.Args[2] if the users only wants to use the validate function. Putting the clientSet function inside main() will make my foo() function broken and I can't declare a variable with an empty value.

So I want my users to be able to run go run main.go validate and go run main.go sync production smoothly.

*production is an arbitrary value

I could have my users to run go run main.go validate _ to plug this problem, but that would be inelegant.What's the best way to tackle this problem?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douquejituan938904
    douquejituan938904 2018-11-08 05:59
    已采纳

    I don't even see the need for a global variable in this case. You can just make the sync function accept a ClientSet e.g. func sync(c ClientSet). But if you really need the global variable then you should not do this unless you want your program to panic when there are no arguments present.

    var (
       clientSet = tools.NewClientSet(os.Args[2])
    )
    

    What you should do is to assign it a default value or the zero value of your type.

    var (
       clientSet tools.ClientSet
    )
    

    Your main function would look somewhat like this:

    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
        }
    
    }
    

    Still, I suggest you just pass a ClientSet to the sync function since it will avoid global variables.

    点赞 评论
  • douqi1212
    douqi1212 2018-11-08 05:50

    The answer is often to not use globals. Instead have foo take an argument foo(clientSet ClientSet) and instantiate it only if you need to.

    点赞 评论
  • down100009
    down100009 2018-11-08 06:36

    Just use len(os.Args) function

    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
        }
    }
    

    Atlhough I would suggest you to NOT to use global variables. Avoid if possible.

    点赞 评论

相关推荐