为什么 map[time.Time]string 有时不起作用?

Why map[time.Time]string doesn't work sometimes?

这是一个当 map[time.Time]string "doesn't work".

时显示的例子
package main

import (
    "fmt"
    "time"
)

type MyDate time.Time

func NewMyDate(year, month, day int, tz time.Location) (MyDate, error) {
    return MyDate(time.Date(year, time.Month(month), day, 0, 0, 0, 0, &tz)), nil
}

func (md MyDate)ToTime() time.Time {
    return time.Time(md)
}

func main()  {
    timeMap := make(map[time.Time]string)

    md1, _ := NewMyDate(2019, 1, 1, *time.UTC)
    md2, _ := NewMyDate(2019, 1, 1, *time.UTC)

    timeMap[md1.ToTime()] = "1"
    timeMap[md2.ToTime()] = "2"

    for k, v := range timeMap {
        fmt.Println(k, v)
    }
}

输出:

2019-01-01 00:00:00 +0000 UTC 1

2019-01-01 00:00:00 +0000 UTC 2

你的时区指针每次都不一样。通过显式提供指针来解决此问题:

func NewMyDate(year, month, day int, tz *time.Location) (MyDate, error) {
    return MyDate(time.Date(year, time.Month(month), day, 0, 0, 0, 0, tz)), nil
}

游乐场:https://play.golang.org/p/M10Xn4jsoKS.

func NewMyDate(year, month, day int, tz time.Location) (MyDate, error) {
  return MyDate(time.Date(year, time.Month(month), day, 0, 0, 0, 0, &tz)), nil
}

&tz指的是NewMyDate参数的地址,每次调用可能不一样。在 Go 中,函数参数按值传递。

每次通话使用相同的时区。例如,

package main

import (
    "fmt"
    "time"
)

type MyDate time.Time

func NewMyDate(year, month, day int, tz *time.Location) (MyDate, error) {
    return MyDate(time.Date(year, time.Month(month), day, 0, 0, 0, 0, tz)), nil
}

func (md MyDate) ToTime() time.Time {
    return time.Time(md)
}

func main() {
    timeMap := make(map[time.Time]string)

    md1, _ := NewMyDate(2019, 1, 1, time.UTC)
    md2, _ := NewMyDate(2019, 1, 1, time.UTC)

    timeMap[md1.ToTime()] = "1"
    timeMap[md2.ToTime()] = "2"

    for k, v := range timeMap {
        fmt.Println(k, v)
    }
}

游乐场:https://play.golang.org/p/M10Xn4jsoKS

输出:

2019-01-01 00:00:00 +0000 UTC 2

地图按预期工作,但是,您的密钥不相等。 如果你添加 fmt.Println(md1.ToTime() == md2.ToTime()) 你会看到。

来自documentation

The comparison operators == and != must be fully defined for operands of the key type; thus the key type must not be a function, map, or slice. If the key type is an interface type, these comparison operators must be defined for the dynamic key values; failure will cause a run-time panic.