After unmarshalling two yaml files to two different maps, i wanted to compare keys (both outer and inner keys as it is a nested map) of both maps and if any key (outer or inner key) is present in first map 'configMap' and absent in second map 'userconfigMap', i wanted to append that key in second map in proper location. Tried iterating over the maps like this but not able to proceed with the implementation as im a newbie to golang.
for k, v := range configMap {
for k1, v1 := range userconfigMap {
if configMap[k] = userconfigMap[k1] {
if configMap[k].(map[string]interface{})[v] =
userconfigMap[k1].(map[string]interface{})[v1] {
}
else {
userconfigMap[k1].append(v)
}
}
}
}
sample yaml files configMap yaml file:
config:
test1: "test1"
test2: "test2"
http_port: 30008
https_port: 32223
userconfigMap yaml file:
config:
test1: "test1"
http_port: 30008
https_port: 32223
Im using map string interface for unmarshalling
You can check if a map key exist in go with
_, found := map[key]
, and add the key to the second map if not: