英文:
Go variable declaration from options
问题
我在node中遇到了这个问题:
var foo = bar || barfoofoo || foooobar;
我该如何在go中实现这个功能。
英文:
I came across this is in node:
var foo = bar || barfoofoo || foooobar;
How could I implement this in go.
答案1
得分: 5
我相信这种做法在Javascript中很常见,因为希望尽量减少发送的代码量。使用这样的简写形式,依赖于字符串的真值,可以编写更短的代码并节省几个字节。
然而,这种做法不是类型安全的,也很棘手:它要求每个阅读你代码的程序员都知道你语言中类型的真值。
我可以理解在Javascript中使用这种做法的理由,但我认为在大多数情况下,你应该避免这种形式。类似的做法也在C中用于验证空值。但除非在你使用的语言中非常惯用,否则不要这样做。保持简单。
回答你的问题 - 如何在Go中实现这个。
这是一个简单的实现:http://play.golang.org/p/EKTP8OsJmR
bar := ""
barfoofoo := ""
foooobar := "omg"
var foo string
if bar != "" {
foo = bar
} else if barfoofoo != "" {
foo = barfoofoo
} else {
foo = foooobar
}
fmt.Printf("foo=%s\n", foo)
输出 foo=omg
。
Go是一种类型安全的语言。字符串没有布尔值,因为它们不是布尔值:
http://play.golang.org/p/r7L8TYvN7c
bar := ""
barfoofoo := ""
foooobar := "omg"
var foo string
if bar {
foo = bar
} else if barfoofoo {
foo = barfoofoo
} else {
foo = foooobar
}
编译器会报错:
prog.go:12: non-bool bar (type string) used as if condition
prog.go:14: non-bool barfoofoo (type string) used as if condition
否则,Go没有三元运算符,所以你不能真正做你想做的事情:
> Go没有三元形式。你可以使用以下方法来实现相同的结果:
if expr {
n = trueVal
} else {
n = falseVal
}
总的来说,你不应该这样做,也不能这样做。
英文:
Preamble
I believe this practice is common in Javascript because of the desire to minimize the amount of code that is sent down the pipe. Using short-forms like that, relying on the truthy value of a string, makes it possible to write shorter code and save a couple of bytes.
However, this sort of practice is not type safe and is tricky: it involves expecting from every programmer reading your code that they know the truth value of the types in your language.
I can see an argument for Javascript, but I believe that in most cases you should avoid this form. Something similar is also used in C to verify null values. But unless very idiomatic in the language you use, don't do that. Keep stuff simple-stupid.
To answer your question - how to do this in go.
Here's the trivial implementation: http://play.golang.org/p/EKTP8OsJmR
bar := ""
barfoofoo := ""
foooobar := "omg"
var foo string
if bar != "" {
foo = bar
} else if barfoofoo != "" {
foo = barfoofoo
} else {
foo = foooobar
}
fmt.Printf("foo=%s\n", foo)
Prints foo=omg
.
Go is a type safe language. Strings don't have a boolean value, because they are not booleans:
http://play.golang.org/p/r7L8TYvN7c
bar := ""
barfoofoo := ""
foooobar := "omg"
var foo string
if bar {
foo = bar
} else if barfoofoo {
foo = barfoofoo
} else {
foo = foooobar
}
The compiler will yell at you:
prog.go:12: non-bool bar (type string) used as if condition
prog.go:14: non-bool barfoofoo (type string) used as if condition
Otherwise, Go doesn't have a ternary operator, so you can't really do what you're trying:
> There is no ternary form in Go. You may use the following to achieve the same result:
if expr {
n = trueVal
} else {
n = falseVal
}
Overall, you shouldn't do that, and you can't do that.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论