doushijia5684 2016-12-01 01:53
浏览 924
已采纳

我们如何在golang中的struct中初始化struct类型的数组(用于存储json输出)

I need to initialize the following data structure which will store a json. The Attack_plans will hold multiple plans and if I loop through the GeneratePlan struct, I need all the plans that were stored.

type GeneratePlan struct {
    Mode         string `json:"mode"`
    Name         string `json:"name"`
    Schema       string `json:"schema"`
    Version      string `json:"version"`
    Attack_plans []struct {
        Attack_plan *Attack_plan `json:"attack-plan"`
    } `json:"attack-plans"`
}
type Attack_plan struct {
    Attack_resources []struct {
        Attack_resource *Attack_resource `json:"attack-resource"`
    } `json:"attack-resources"`
}

Can anyone please suggest something? If the data structure needs to be simplified before initializing it, then please suggest that as well. I am very new to golang so please ignore the best practices to follow. Any help is appreciated. Thanks!

  • 写回答

3条回答 默认 最新

  • douqinlu4217 2016-12-07 22:43
    关注

    I found the solution! This simplifies the above data structure!

    type GeneratePlan struct{
        Mode    string `json:"mode"`
        Name    string `json:"name"`
        Schema  string `json:"schema"`
        Version string `json:"version"`
        Attack_plans []struct1 `json:"attack-plans"`
    
    } 
    
    type struct1 struct {
        Attack_plan Attack_plan `json:"attack-plan"`
    }
    
    
    type Attack_plan struct{
        Attack_resouces []struct2 `json:"attack-resources"`
    }
    
    type struct2 struct {
        Attack_resource Attack_resource `json:"attack-resource"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?