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

我们如何在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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?