【发布时间】: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