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 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?