比较GO中的地图值

I have to compare the values (not keys) of two maps of type 'map[string]float64'.

The contents of the maps are : map1[ABCD:300 PQRS:400] and map2[ABCD:30 PQRS:40] No I do a check like if value(map1)/value(map2) > = 1 (like 300/30=10>1), then do something.

How can I achieve this in GO ? TIA.

i tried something like this :

for key := range m2{
            for k := range m1{
                temp := m1[k] / m2[key]
                fmt.Println("temp*******", temp)
            }
        }

Playground working example

Breaking down the main for loop:

for key, value1 := range m1 {
    if value2, ok := m2[key]; ok {
        fmt.Printf("%f / %f = %f
", value1, value2, value1/value2)
        if value2 != 0 && value1/value2 > 1 {
            fmt.Println("Greater than 1!")
        } else {
            fmt.Println("Not greater than 1!")
        }
    }
}

First I use range to pull out both a value and a key for every entry in m1, since we need both.

Second, using the comma ok syntax of value2, ok := m2[key], I'm both finding out what the associated value of the second map is for the given key, and also ensuring that that map entry exists when the surrounding if ... ; ok. From your description of the problem, it isn't necessary to check every element of the second map, only those that share keys with the first. It's important we make this check, since go doesn't throw errors when checking a map for a non-existent key, it just silently returns the zero-value of the variable--in this scenario that could mean dividing by zero later on.

Third, well, just some formatted printlines to help see what's happening, the real juice is the very simple if value1/value2 > 1 statement. Note that I also added in my playground link some map entries that don't fulfill the requirement to help demonstrate that the behavior is correct.

The "go maps in action" blog article linked in the other answer is indeed a great source for working with maps in go.