Ruby或等于的golang版本|| =
问题描述:
Out of curiosity, what would be the idiomatic way to assign a variable if it's nil in go?
I'm looking for something similar to ruby's foo ||= bar
Is there a shorter version of this?
if foo == nil{
foo = bar
}
出于好奇,如果变量go变为nil,惯用的方式是什么? p>
我正在寻找与ruby的 类似的东西吗? p>
foo || = bar code> p> p>
如果foo == nil {
foo = bar
}
code> pre>
div>
答
Your version is already the simplest, shortest and idiomatic way to do it.