为什么Go没有三元运算符

Why does Go not have the ?: operator?
为什么Go没有?:运算符?

There is no ternary testing operation in Go.
Go中没有三元测试操作。

You may use the following to achieve the same result:
您可以使用以下方法获得相同的结果:

if expr {
    n = trueVal
} else {
    n = falseVal
}

The reason ?: is absent from Go is that the language’s designers had seen the operation used too often to create impenetrably complex expressions.
Go缺少?:的原因是该语言的设计师发现该操作过于频繁地用于创建难以理解的复杂表达式。

The if-else form, although longer, is unquestionably clearer.
if-else形式虽然更长,但无疑更清晰。

A language needs only one conditional control flow construct.
一种语言仅需要一个条件控制流构造。

发布了636 篇原创文章 · 获赞 147 · 访问量 22万+

猜你喜欢

转载自blog.csdn.net/weixin_43336281/article/details/102146617