I have two Maps:
First one:
map[11:manufacturer 2:upc 5:short_description 10: 4:category 6: 7:url
8:image 9: 0:name 1:mpn 3:sku]
Second one:
map[3:manufacturer 5:mpn 8:category_path 10:is_in_stock 2:final_price
1:name 4:short_description 6:thumbnail 7:url 9:furniture_type 0:sku]
map[news_to_date:2014-10-20 00:00:00 url_key:zanbury-panel-storage-
bedroom-set price:0 media_gallery:[map[value_id:507779 file:/b/2/b217-
31-36-46-57-54s-95-92.jpg label:<nil> disabled:0 position:1]
map[label:<nil> disabled:0 position:2 value_id:507777 file:/b/2/b217-
57-54s-95_1.jpg] map[value_id:507778 file:/b/2/b217-57-detail_3.jpg
label:<nil> disabled:0 position:3] map[value_id:507780 file:/b/2/b217-
54s-detail_1.jpg label:<nil> disabled:0 position:4]
map[file:/b/2/b217-handle_1.jpg label:<nil> disabled:0 position:5
value_id:507781] map[value_id:507782 file:/b/2/b217-92-sw_1.jpg label:
<nil> disabled:0 position:6] map[value_id:507783 file:/b/2/b217-31-36-
sw_-_copy_2_.jpg label:<nil> disabled:0 position:7]
map[value_id:507784 file:/b/2/b217-46-sw_1.jpg label:<nil> disabled:0
position:8]] sku_8:<nil> sku_1:<nil> shipment_type:0 url_path:zanbury-
panel-storage-bedroom-set.htm custom_design:<nil> sku_5:<nil> upc:
<nil> special_from_date:<nil> mk_expecdate:<nil> sku_type:1
has_options:1 price_view:0 jet_price:<nil> categories:[11 10809]
How I can compare both and select those values from the second one which matches the first one.
If I understand your question correctly, the following should work:
Step I. Reverse keys and values in your 2nd map: reversed_map_2
. This assumes that the values are unique.
Step II. Loop over values in your 1st map, now you can look up the values in reversed_map_2
I did not understand much of your second maps. But my understanding of your question is that you want to extract the unique elements from the maps.
The result which I extracted is based on the value but not the key. It shows you how to compare 2 maps and have the result in the form of (key, value).
func unique(w http.ResponseWriter, r *http.Request) {
m1 := map[int]string{
11: "manufacturer",
2: "upc",
5: "short_description",
10: "",
4: "category",
6: " ",
7: "url",
8: "image",
9: "",
0: "name",
1: "mpn",
3: "sku",
}
m2 := map[int]string{
3: "manufacturer",
5: "mpn",
8: "category_path",
10: "is_in_stock",
2: "final_price",
1: "name",
4: "short_description",
6: "thumbnail",
7: "url",
9: "furniture_type",
0: "sku",
}
for key, val := range m1 {
found := false
for key1, val1 := range m2 {
if val == val1 {
found = true
fmt.Println("second map")
fmt.Println("key:", key1, "val:", val1)
break
}
}
if found {
fmt.Println("first map")
fmt.Println("key:", key, "val:", val)
}
}
}
The output will be :
second map
key: 5 val: mpn
first map
key: 1 val: mpn
second map
key: 0 val: sku
first map
key: 3 val: sku
second map
key: 3 val: manufacturer
first map
key: 11 val: manufacturer
second map
key: 1 val: name
first map
key: 0 val: name
second map
key: 4 val: short_description
first map
key: 5 val: short_description
second map
key: 7 val: url
first map
key: 7 val: url
In order to show the clear difference between matched elements based on the value I used if found.