dongque6377 2019-08-14 20:48
浏览 5
已采纳

我不了解此处的评估规则[关闭]

I am confused about a syntax usage in Go.

func f(){
    m := map[int]string{1: "one", 2: "two"}
    if x, ok := m[3]; !ok{
        // do something
    }
}

I understand what if x, ok := map[3] does, but I am confused about the difference between either have a ; !ok or a ; ok at the end, and the meaning of those two.

By the way, is it valid if I only write if x, ok := map[3] without a ; that extends it?

Thank you!

  • 写回答

1条回答 默认 最新

  • dtrz17917 2019-08-14 21:00
    关注

    By the way, is it valid if I only write if x, ok := map[3] without a ; that extends it?

    No, this is a syntax error, because x, ok := map[3] is not a boolean expression. In fact, it's not even an expression; a short variable declaration is a statement. So it can't be the expression that controls an if.

    but I am confused about the difference between either have a ; !ok or a ; ok at the end, and the meaning of those two.

    The same as for any if. If you want the block to run when ok is true, you use if ok. If you want the block to run when ok is false, you use if !ok. The only difference is that we've put the statement that gives ok its value between the word if and the test.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
  • ¥15 Android studio AVD启动不了
  • ¥15 陆空双模式无人机怎么做
  • ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关
  • ¥15 C#中的编译平台的区别影响
  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译