【问题标题】:Golang syntax in "if" statement with a map带有映射的“if”语句中的 Golang 语法
【发布时间】:2018-10-08 15:50:17
【问题描述】:

我正在这里阅读教程:http://www.newthinktank.com/2015/02/go-programming-tutorial/

在“地图中的地图”部分有:

package main

import "fmt"

func main() {

    // We can store multiple items in a map as well

    superhero := map[string]map[string]string{
        "Superman": map[string]string{
            "realname":"Clark Kent",
            "city":"Metropolis",
        },

        "Batman": map[string]string{
            "realname":"Bruce Wayne",
            "city":"Gotham City",
        },
    }

    // We can output data where the key matches Superman

    if temp, hero := superhero["Superman"]; hero {
        fmt.Println(temp["realname"], temp["city"])
    }

}

我不明白“如果”语句。有人可以告诉我这一行的语法吗:

if temp, hero := superhero["Superman"]; hero {

if temp 对局外人来说似乎是荒谬的,因为 temp 甚至没有在任何地方定义。那甚至会完成什么?然后hero := superhero["Superman"] 看起来像一个作业。但是分号在做什么呢?为什么最后的hero在那里?

有人可以帮助新手吗?

非常感谢。

【问题讨论】:

标签: dictionary if-statement go syntax


【解决方案1】:
if temp, hero := superhero["Superman"]; hero

在go中类似于写:

temp, hero := superhero["Superman"]
if hero {
    ....
}

这里是“超人”被映射到一个值,英雄将是true

否则false

在 go 中,对 map 的每个查询都会返回一个可选的第二个参数,该参数将判断某个键是否存在

https://play.golang.org/p/Hl7MajLJV3T

【讨论】:

    【解决方案2】:

    一个二值赋值测试一个键的存在:

    i, ok := m["route"]
    

    在这个语句中,第一个值 (i) 被赋值为存储的值 在“路线”键下。如果该键不存在,则 i 是值 类型的零值 (0)。第二个值(ok)是一个布尔值,如果 键存在于地图中,如果不存在则为假。

    这个检查基本上是在我们没有确认地图里面的数据的时候使用的。所以我们只检查一个特定的键,如果它存在,我们将值分配给变量。这是一个 O(1) 检查。

    在您的示例中,尝试在地图中搜索不存在的键:

    package main
    
    import "fmt"
    
    func main() {
    
        // We can store multiple items in a map as well
    
        superhero := map[string]map[string]string{
            "Superman": map[string]string{
                "realname": "Clark Kent",
                "city":     "Metropolis",
            },
    
            "Batman": map[string]string{
                "realname": "Bruce Wayne",
                "city":     "Gotham City",
            },
        }
    
        // We can output data where the key matches Superman
    
        if temp, hero := superhero["Superman"]; hero {
            fmt.Println(temp["realname"], temp["city"])
        }
    
        // try to search for a key which doesnot exist
    
        if value, ok := superhero["Hulk"]; ok {
            fmt.Println(value)
        } else {
            fmt.Println("key not found")
        }
    
    }
    

    Playground Example

    【讨论】:

      【解决方案3】:

      布尔变量名使用 ok 更正常。这相当于:

      temp, ok := superhero["Superman"]
      if ok {
          fmt.Println(temp["realname"], temp["city"])
      }
      

      如果地图中有键,则该确定为真。因此,语言中内置了两种形式的地图访问,以及这种语句的两种形式。我个人认为这种稍微冗长的形式加上一行代码会更清晰,但你可以使用任何一种。所以另一种形式是:

      if temp, ok := superhero["Superman"]; ok {
          fmt.Println(temp["realname"], temp["city"])
      }
      

      如上。更多信息请参见effective go

      出于显而易见的原因,这被称为“comma ok”成语。在这个 例如,如果存在密钥,则将正确设置该值并且可以 将是真实的;如果不是,则该值将设置为零并且 ok 将是 错误的。

      访问地图的两种形式是:

      // value and ok set if key is present, else ok is false
      value, ok := map[key]
      
      // value set if key is present
      value := map[key]
      

      【讨论】:

      • 所以,无论哪种情况,我都可以看到temp 必须分配给superhero["Superman"]。在您的代码中,另一个变量在做什么更清楚。但是,ok 究竟被分配到什么位置……为什么?这种语法还能在 Golang 中如何出现?
      • 阅读有效的 go(上面链接)以获取更多信息。这被称为逗号,好的成语。如果地图中有此键的值,则 ok 被赋值为 true,否则为 false(在这种情况下,“超人”是键)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-28
      • 2020-09-20
      • 1970-01-01
      • 2021-12-20
      • 2017-12-29
      • 2015-01-21
      • 1970-01-01
      相关资源
      最近更新 更多