lundi 8 octobre 2018

Golang syntax in "if" statement with a map

I am reading a tutorial here: http://www.newthinktank.com/2015/02/go-programming-tutorial/

On the "Maps in Maps" section it has:

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"])
    }

}

I don't understand the "if" statement. Can someone walk me through the syntax on this line:

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

Like if temp seems nonsensical to an outsider as temp isn't even defined anywhere. What would that even accomplish? Then hero := superhero["Superman"] looks like an assignment. But what is the semicolon doing? why is the final hero there?

Can someone help a newbie out?

Many thanks.

Aucun commentaire:

Enregistrer un commentaire