doufen2769 2018-12-17 18:48
浏览 18
已采纳

如何初始化特定的结构格式

This question already has an answer here:

Although I've been using language for more than a couple of months now, I hadn't seen myself with this need. What I want is to initialize the structure that I show below (basically initialize Project and then, inside, aaa, bbb and ccc structs). My question is whether I can really initialize it without making an unmarshall.

type example struct {
    Name    string `yaml:"name" json:"name"`
    Key     string `yaml:"key" json:"key"`
    Version string `yaml:"version" json:"version"`
    Project []struct {
        Name string `yaml:"name" json:"name"`
        aaa  struct {
            Name    string `yaml:"name" json:"name"`
            Key     string `yaml:"key" json:"key"`
            xxx struct {
                Version string `yaml:"version" json:"version"`
            } `yaml:"xxx" json:"xxx"`
            zzz struct {
                Version string `yaml:"version" json:"version"`
            } `yaml:"zzz" json:"zzz"`
        } `yaml:"aaa" json:"aaa"`
        bbb struct {
            Name    string `yaml:"name" json:"name"`
            Key     string `yaml:"key" json:"key"`
            xxx struct {
                Version string `yaml:"version" json:"version"`
            } `yaml:"xxx" json:"xxx"`
            zzz struct {
                Version string `yaml:"version" json:"version"`
            } `yaml:"zzz" json:"zzz"`
        } `yaml:"bbb" json:"bbb"`
        ccc struct {
            Name    string `yaml:"name" json:"name"`
            Key     string `yaml:"key" json:"key"`
            xxx struct {
                Version string `yaml:"version" json:"version"`
            } `yaml:"xxx" json:"xxx"`
            zzz struct {
                Version string `yaml:"version" json:"version"`
            } `yaml:"zzz" json:"zzz"`
        } `yaml:"ccc" json:"ccc"`
    } `yaml:"project" json:"project"`
}

I know I could do it in some way similar to the following. But I try to know the language better.

type example struct {
    Name    string `yaml:"name" json:"name"`
    Project proyectos }

type proyectos structs{
    [] proyecto
}

type proyecto struct{
    Name string `yaml:"name" json:"name"`
    aaa  struct {
    .....
} `yaml:"project" json:"project"`

I appreciate anyone who can give me a hint about this with an example.

Thank you.

</div>
  • 写回答

1条回答 默认 最新

  • doushenmao9036 2018-12-17 18:55
    关注

    That actually looks right. I'd prefer to remove the proyectos struct definition as it's unnecessary in my opinion - you can just use []proyecto instead, which looks cleaner.

    I hate to be that guy, but your question has already been answered here: https://stackoverflow.com/a/24809404/7471182

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数