Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maps with time.Time keys #22977

Closed
pjebs opened this issue Dec 3, 2017 · 6 comments
Closed

Maps with time.Time keys #22977

pjebs opened this issue Dec 3, 2017 · 6 comments

Comments

@pjebs
Copy link
Contributor

pjebs commented Dec 3, 2017

If I have a map with a time.Time type as a key, or alternatively a struct with a time.Time type as a field, with the struct used as a key, it appears as if the key comparisons use == instead of the Equal method as recommended by: https://golang.org/pkg/time/#Time.Equal

Equal reports whether t and u represent the same time instant. Two times can be equal even if they are in different locations. For example, 6:00 +0200 CEST and 4:00 UTC are Equal. See the documentation on the Time type for the pitfalls of using == with Time values; most code should use Equal instead.

@pjebs
Copy link
Contributor Author

pjebs commented Dec 3, 2017

eg.

map[time.Time]int

OR 

type A struct {
t time.Time
}

map[A]int

@mvdan
Copy link
Member

mvdan commented Dec 3, 2017

Maps don't call any functions or methods, they use equality as defined in the spec. What is your point here?

@pjebs
Copy link
Contributor Author

pjebs commented Dec 3, 2017

#21829

@pjebs pjebs closed this as completed Dec 3, 2017
@go101
Copy link

go101 commented Dec 4, 2017

@pjebs
You should use time.Duration as the struct field type, and use aTime.Sub(time.Time{}) as the struct field values to make the struct values capable to act as map keys.

@go101
Copy link

go101 commented Dec 4, 2017

filed a related issue: #22978

@richardartoul
Copy link

@pjebs I have a linter for this issue if you're interested: https://github.com/m3db/build-tools/blob/master/linters/badtime/README.md

@golang golang locked and limited conversation to collaborators Dec 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants