在go中声明一个没有值的全局变量
Declaring a global variable without a value in go
我有一个程序需要 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
变量是全局的,但如果用户只想使用验证功能,我不需要用户拥有 os.Args[2]。将 clientSet
函数放在 main()
中会使我的 foo()
函数损坏,并且我无法声明具有空值的变量。
所以我希望我的用户能够运行go run main.go validate
和go run main.go sync production
顺利。
*production 为任意值
我可以让我的用户 运行 go run main.go validate _
来解决这个问题,但那将是 inelegant.What 解决这个问题的最佳方法?
答案通常是不使用全局变量。相反,让 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
}
}
不过,我还是建议您将 ClientSet
传递给同步函数,因为它会避免使用全局变量。
只需使用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
}
}
尽管我建议您不要使用全局变量。尽可能避免。
我有一个程序需要 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
变量是全局的,但如果用户只想使用验证功能,我不需要用户拥有 os.Args[2]。将 clientSet
函数放在 main()
中会使我的 foo()
函数损坏,并且我无法声明具有空值的变量。
所以我希望我的用户能够运行go run main.go validate
和go run main.go sync production
顺利。
*production 为任意值
我可以让我的用户 运行 go run main.go validate _
来解决这个问题,但那将是 inelegant.What 解决这个问题的最佳方法?
答案通常是不使用全局变量。相反,让 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
}
}
不过,我还是建议您将 ClientSet
传递给同步函数,因为它会避免使用全局变量。
只需使用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
}
}
尽管我建议您不要使用全局变量。尽可能避免。