Go变量声明的选项

huangapple go评论90阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2013年8月4日 11:05:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/18039351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定