Golang map[string]float 其中 float 被覆盖,而不是添加到键的现有值

发布于 2025-01-10 03:03:30 字数 1377 浏览 0 评论 0原文

我有一个像这样的字符串 00:01:07,400-234-090 00:05:01, 701-080-080 00:05:00, 400-234-090

其中右侧是电话号码,左边是通话的持续时间,格式为 hh:mm:ss。我试图将其放入 map[string]float64 中,方法是首先在“,”上分割字符串,然后在“:”上分割左侧。然后根据通话持续时间计算持续时间并转换为分钟。到此为止一切正常。 现在我尝试将其放入地图中,我期望如果右侧的电话号码键已存在于地图中,那么它只会将 float64 值添加到键的现有值中。然而,事实并非如此,在地图中似乎有两次相同的密钥。

这是我的代码:

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)

https://go.dev/play/p/fLcEDbgQ-7q

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)

https://go.dev/play/p/fLcEDbgQ-7q

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇妻 2025-01-17 03:03:30

通过修剪持续时间和数字上的空格来修复。当前代码不处理逗号之前或之后的空格。

    i := strings.Index(v, ",")
    if i < 0 {
        log.Fatal("bad line", v)
    }
    dur := strings.TrimSpace(v[:i])
    num := strings.TrimSpace(v[i+1:])

利用映射对于缺失键返回零值的事实,更新映射的代码可以简化为以下内容。

    mapDetails[num] += total

在 Playground 上运行代码

在调试解析字符串的代码时,通过使用 %q 打印来使空格可见会很有帮助。原始程序中的错误通过以下方式更加明显:

    fmt.Printf("phone number is: %q \n", callDetails[1])

Fix by trimming spaces on the duration and the number. The current code does not handle spaces before or after the comma.

    i := strings.Index(v, ",")
    if i < 0 {
        log.Fatal("bad line", v)
    }
    dur := strings.TrimSpace(v[:i])
    num := strings.TrimSpace(v[i+1:])

Taking advantage of the fact that maps return the zero value for missing keys, the code to update the map can be simplified to the following.

    mapDetails[num] += total

Run the code on the playground.

When debugging code that parses strings, it's helpful to make whitespace visible by printing with %q. The bug in the original program is more visible with:

    fmt.Printf("phone number is: %q \n", callDetails[1])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文