I have a string like so
00:01:07,400-234-090 00:05:01, 701-080-080 00:05:00, 400-234-090
where the on the right side is the phone number and on the left is the duration of the call in hh:mm:ss format. I am trying to put this in a map[string]float64 by splitting the string first on "," and the split the left side on ":". Then make a Duration from the duration of the call and convert in to minutes. It works fine till this. Now I am trying to put this in a map, I expected that if the key which is the phone number on the right is already present in the map then it will just add the float64 value to the existing value of the key. However, that is not the case, it seems to be having the same key twice in the map.
Here is my code:
phoneBill := `00:01:07,400-234-090
00:05:01, 701-080-080
00:05:00, 400-234-090`
callLog := strings.Split(phoneBill, "\n")
mapDetails := make(map[string]float64)
for _, v := range callLog {
callDetails := strings.Split(strings.TrimSpace(v), ",")
timeDetails := strings.Split(strings.TrimSpace(callDetails[0]), ":")
durationString := strings.TrimSpace(timeDetails[0]) + "h" + strings.TrimSpace(timeDetails[1]) + "m" + strings.TrimSpace(timeDetails[2]) + "s"
t, _ := time.ParseDuration(durationString)
total := t.Minutes()
fmt.Printf("phone number is: %v \n", callDetails[1])
fmt.Printf("duration of call in minutes is %v \n", total)
if v, found := mapDetails[callDetails[1]]; found {
total += v
fmt.Println("total is :v", total)
}
mapDetails[(callDetails[1])] = total
}
fmt.Println("Values in map are: %v", mapDetails)