0

这是我的代码:

package main

import (
    "fmt"
)
type person struct {
        //name  [][]string{};
        name [][]string
    }  

func main() {
var people = map[string]*person{}

people["first person"] = &person{name:{{"My name","30"}}}
    fmt.Println(people["first person"])
}

我有一个错误:

复合文字中缺少类型

我想要输出为[[My name,30]]

有人可以帮助我吗?

4

2 回答 2

1

这是工作示例。您必须在使用前声明组合文字的类型。

package main

import (
    "fmt"
)

type person struct {
    //name  [][]string{};
    name [][]string
}

func main() {
    var people = map[string]*person{}

    people["first person"] = &person{name: [][]string{{"John", "30"}}}
    fmt.Println(people["first person"])
}
于 2018-04-13T13:16:44.203 回答
1

type在创建实例指针并对其进行初始化时丢失了,它应该是:

&person{name: [][]string{{"My name, 30"}}}

下面是工作示例:

package main

import (
    "fmt"
)

type person struct {
    name [][]string
}

func main() {
    var people = map[string]*person{}
    people["first person"] = &person{name: [][]string{{"My name, 30"}}}
    fmt.Println(people["first person"].name)
}
于 2018-04-13T17:48:10.953 回答