cloudaice / learnGo

there are something about Go Useage
MIT License
0 stars 0 forks source link

当结构体遇上Map #8

Open cloudaice opened 10 years ago

cloudaice commented 10 years ago
package main

import "fmt"

type MyType struct {
    Name string
    Age string
}

func main() {
    result := make(map[string]MyType)
    result["name"] = MyType{
        Name: "xiangcha
        Age: "23",
    }
    result["name"].Name = "xiangchaochao"
    fmt.Println(result)
}

上面这段代码编译不通过,下面这段代码缺可以正常编译通过

package main

import "fmt"

type MyType struct {
    Name string
    Age string
}

func main() {
    result := make(map[string]MyType)
    result["name"] = MyType{
        Name: "xiangcha
        Age: "23",
    }
    name := result["name"]
    name.Name = "xiangchaochao"
    result["name"] = name
    fmt.Println(result)
}