1

In Python z = x or y can be understood as assign z as if x is falsey, then y, else x, is there a similar idiom in golang?

Specifically the two variables on the right are strings, I'd like to assign the first if it's non-emtpy, otherwise the second.

cerberos
  • 7,705
  • 5
  • 41
  • 43
  • Are you asking about a ternary operator? If so, see here for the idiomatic way to do conditionals. http://stackoverflow.com/questions/19979178/what-is-idiomatic-gos-equivalent-of-cs-ternary-operator – Antiga Dec 07 '15 at 22:35
  • Go is statically-typed. There isn't a concept of "falsey" in any type other than `bool`. – newacct Dec 08 '15 at 02:46

1 Answers1

4

No, you have to use if/else:

s1, s2 := "", "hello"
var x string

if s1 == "" {
    x = s2
} else {
    x = s1
}
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753