drot98385 2014-07-27 09:24
浏览 4046
已采纳

如何在Golang中正确使用OR运算符

How can i do this simplified in Golang

var planningDate string
  date, ok := data["planningDate"]
  if !ok {
    planningDate = util.TimeStamp()
  } else {
    planningDate = date
  }

Thanx

  • 写回答

2条回答 默认 最新

  • dpa31905 2014-07-27 09:31
    关注

    I don't see any way to do this in a single line, as there is no ternary operator in Go. You cannot use | either as operands are not numbers. However, here is a solution in three lines (assuming date was just a temporary variable):

    planningDate, ok := data["planningDate"]
    if !ok {
        planningDate = util.TimeStamp()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?