小编use*_*176的帖子

带地图的“if”语句中的 Golang 语法

我正在这里阅读教程: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"])
    }

}
Run Code Online (Sandbox Code Playgroud)

我不明白“如果”语句。有人可以引导我完成这一行的语法吗:

if temp, hero := superhero["Superman"]; hero {
Run Code Online (Sandbox Code Playgroud)

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

有人可以帮助新手吗?

非常感谢。

syntax dictionary if-statement go

4
推荐指数
1
解决办法
7532
查看次数

大写是否会影响 golang 功能块内的变量?

如果我在函数中声明一个变量或参数,大写是否有任何影响或意义?

显然,对于函数之外的方法和变量,它会导出它们,但是上面的呢?

variables go scopes

0
推荐指数
1
解决办法
351
查看次数

标签 统计

go ×2

dictionary ×1

if-statement ×1

scopes ×1

syntax ×1

variables ×1