I have a JSON file looks like following somehow
{
"type": "Weekly",
"clients": [
"gozo",
"dva"
],
"sender": "no-reply@flowace.in",
"recipients": {
"gozo": [
"a@gmail.com",
"b@hotmail.com"
],
"dva": [
"c@gmail.com",
"d@hotmail.com"
]
},
"features": [
"Top5UsedApps",
"TimeSpentOnEachL3",
"NewlyAssignedL3",
"HoursLogged"
],
"dbCloning": [
"dva"
]
}
I have mapped structs like the following.
type receivers struct {
Gozo []string `json:"gozo"`
Dva []string `json:"dva"`
// Add more recievers
}
// Config -- The config object parsed from the JSON file
type Config struct {
ReportType string `json:"type"`
Clients []string `json:"clients"`
Sender string `json:"sender"`
Recipients receivers `json:"recipients"`
Cloning []string `json:"dbCloning"`
}
Then somewhere in the another source file, I do the following,
func main() {
conf := LoadConfig(os.Args[1])
for _, client := range conf.Clients {
// Use the client variable of some other function calls
fmt.Println(conf.Recipients[client]) // This will not work
}
Now my question is how can I make it work. I can not loop over conf.Recipients
directly.
PS: Consider that LoadConfig
function unmarshal the JSON and returns a conf
object.
Edit 1: It looks like it was design decision error. Now going with the solution of map[string][]string
. But not marking it as an answer because the need to know how it is easily done for all the cases where there was no other choices.