在"if"语句中使用Golang的map语法

4

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

在“Maps in Maps”部分中,它有:

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" 语句。有人能帮我解释一下这行代码的语法吗?
if temp, hero := superhero["Superman"]; hero {

if temp 对于外行人来说似乎毫无意义,因为 temp 甚至没有在任何地方定义。那会有什么作用呢?然后 hero := superhero["Superman"] 看起来像是一个赋值语句。但分号在做什么?最后的 hero 又是什么意思呢?

有人能帮助一下新手吗?

非常感谢。


2
“map查找的两个值形式在教程中有所涉及”,同时也包括“if条件之前的语句”在内。 - Peter
为什么不参加Go之旅? - Volker
3个回答

5

一个两值赋值测试键是否存在:

i, ok := m["route"]

在这个语句中,第一个值(i)被赋予存储在键“route”下的值。如果该键不存在,则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


请参阅“利用零值”:https://go.dev/blog/maps。 - Brent Bradburn

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

在 Go 中编写代码类似于:

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

这里的"Superman"被映射到一个值,如果是英雄,true将被返回,否则返回false

在Go语言中,对于映射的每个查询,都会返回一个可选的第二个参数,该参数将告诉您某个键是否存在。

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


0

使用“ok”作为布尔变量名更为常见。这与以下代码等效:

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

如果地图中有一个键,则ok为真。因此,语言内置了两种地图访问形式和两种此语句的形式。个人认为,这种稍微冗长一些的形式,多了一行代码,更加清晰明了,但您可以使用任何一种形式。因此,另一种形式是:

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

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

出于显而易见的原因,这被称为“逗号OK”习语。在此示例中,如果键存在,则值将被适当设置,并且ok将为true;如果不存在,则该值将被设置为零,并且ok将为false。

访问地图的两种形式是:

// 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中? - user40176
请阅读上面链接的《Effective Go》以获取更多信息。这被称为“逗号OK惯用语”。如果地图中存在此键的值,则ok被赋值为true;否则,如果不存在,则为false(在本例中,“超人”是该键)。 - Kenny Grant

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接