I have searched a lot about it but couldn't find the proper solution. what I am trying to do is to create the following as final output using arrays and slices in golang.
[
11 => [1,2,3],
12 => [4,5],
]
what I have implemented is:
type Industries struct {
IndustryId int `json:"industry_id"`
FormIds []int `json:"form_ids"`
}
var IndustrySettings IndustrySettings
_ := json.NewDecoder(c.Request.Body).Decode(&IndustrySettings)
var industryArr []int
for _, val := range IndustrySettings.IndustrySettings {
industryArr = append(industryArr, val.IndustryId)
}
In this IndustrySettings
contains following json
{
"industry_settings": [{
"industry_id": 11,
"form_ids": [1, 2, 3]
},
{
"industry_id": 12,
"form_ids": [4, 5]
}
]
}
I want to loop through this json and convert into the array like industry_id as key and form_ids as values.
Can anyone please tell how to accomplish this?
Thanks!
Edit
I mean I need output like
[
11 => [1,2,3],
12 => [4,5],
]
where 11
and 12
are the industry_id as given in the json to be used as key of the array and [1,2,3]
, [4,5]
are the form ids to be set as values in the array.