我正在阅读这里的教程: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
又是什么意思呢?
有人能帮助一下新手吗?
非常感谢。