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条)

报告相同问题?