如何在Golang中初始化嵌套结构?

immrsteel:
type important struct {
client     string                 `json:"client"`
Response   Summary        `json:"response"`

}

type Summary struct {
Name     string           `json:"name"`
Metadata Clientdata      `json:"metadata"`
}

type Clientdata struct {
Income string           `json:"income"` 
}


v := &important{ client: "xyz", Response:  Summary[{
            Name: "test",
            Metadata: Clientdata { "404040"},
        }
    }]

//错误:无法使用摘要{名称:“ test”,元数据:Clientdata {“ 404040”},}(类型摘要)作为类型[]摘要更多...

我在这里做错了什么?

米洛·克里斯蒂安森(Milo Christiansen):

简而言之,您对切片文字的语法稍作改动。您的错误是很合逻辑的,但可悲的是它不起作用。

以下是固定版本:

v := &important{ client: "xyz", Response: []Summary{
        {
            Name: "test",
            Metadata: Clientdata { "404040"},
        },
    },
}

切片文字的定义如下:

[]type{ items... }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章