gpt4 book ai didi

go - 无法使用 if val 从 Golang 中的 map[time.Time]Measure 获取值,ok := mapMeasures[ts]; ok {}

转载 作者:行者123 更新时间:2023-12-01 20:23:31 27 4
gpt4 key购买 nike

我有一个定义如下的 map :

mapMeasures := make(map[time.Time]models.Measure, 0)

type Measure struct {
Delta float64 // I just let one field to simplificate
}

因此初始循环将填充从 22/01/20-10:10:0022/01/20-12:00:00 的值,所以它将存储 12 个键值(10 分钟时间步长)

然后,它将再次循环那些时间戳,并将增量添加到现有值。

因此,我需要检查是否已经存在与我的实际时间戳对应的 key :

if val, ok := mapMeasures[ts]; ok { // ts already exists, we must sum delta values
measure.Delta += val.Delta
}

但似乎这种情况永远不会成立。

我调试了代码,我可以看到时间戳实际上存在于 map 中:

 mapMeasures = {map[time.Time]gitlab.com/company/common/models.Measure} 
0 = ->
key = {time.Time} 2020-01-22 11:40:00 +0100
value = {*gitlab.com/company/common/models.Measure | 0xc000132460}
1 = ->
key = {time.Time} 2020-01-22 12:30:00 +0100
value = {*gitlab.com/company/common/models.Measure | 0xc000132780}
2 = ->
key = {time.Time} 2020-01-22 12:50:00 +0100
value = {*gitlab.com/company/common/models.Measure | 0xc0001328c0}
3 = ->
key = {time.Time} 2020-01-22 11:00:00 +0100
value = {*gitlab.com/company/common/models.Measure | 0xc000132140}
4 = ->
key = {time.Time} 2020-01-22 11:10:00 +0100
value = {*gitlab.com/company/common/models.Measure | 0xc000132280}
5 = ->
key = {time.Time} 2020-01-22 11:20:00 +0100
value = {*gitlab.com/company/common/models.Measure | 0xc000132320}
6 = ->
key = {time.Time} 2020-01-22 11:30:00 +0100
value = {*gitlab.com/company/common/models.Measure | 0xc0001323c0}
7 = ->
key = {time.Time} 2020-01-22 11:50:00 +0100
value = {*gitlab.com/company/common/models.Measure | 0xc000132500}
8 = ->
key = {time.Time} 2020-01-22 12:00:00 +0100
value = {*gitlab.com/company/common/models.Measure | 0xc0001325a0}
9 = ->
key = {time.Time} 2020-01-22 12:10:00 +0100
value = {*gitlab.com/company/common/models.Measure | 0xc000132640}
10 = ->
key = {time.Time} 2020-01-22 12:20:00 +0100
value = {*gitlab.com/company/common/models.Measure | 0xc0001326e0}
11 = ->
key = {time.Time} 2020-01-22 12:40:00 +0100
value = {*gitlab.com/company/common/models.Measure | 0xc000132820}

实际ts:

{time.Time} 2020-01-22 11:00:00 +0100

将 key 作为时间戳有什么问题吗?我应该将其转换为字符串还是整数???

最佳答案

引自time.Time :

Note that the Go == operator compares not just the time instant but also the Location and the monotonic clock reading. Therefore, Time values should not be used as map or database keys without first guaranteeing that the identical Location has been set for all values, which can be achieved through use of the UTC or Local method, and that the monotonic clock reading has been stripped by setting t = t.Round(0). In general, prefer t.Equal(u) to t == u, since t.Equal uses the most accurate comparison available and correctly handles the case when only one of its arguments has a monotonic clock reading.

不要使用 time.Time 作为映射键,而是使用 Time.Unix() 返回的 Unix 时间戳. Unix 时间戳是位置和单调时钟读取“免费”。

如果您的 key 还必须包含位置(时区),则使用包含 Unix 时间戳和时区偏移量的结构,例如:

type Key {
ts int64
offset int
}

参见相关问题:Why do 2 time structs with the same date and time return false when compared with ==?

关于go - 无法使用 if val 从 Golang 中的 map[time.Time]Measure 获取值,ok := mapMeasures[ts]; ok {},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60989254/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com