-
Notifications
You must be signed in to change notification settings - Fork 18k
time: add Time.GoString method to help %#v [freeze exception] #39034
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
Comments
Ah, |
I'm not really sure that needs to be in the stdlib, it's trivial to implement without |
If your struct is type A struct {
B time.Time
}
a := &A{B: time.Now()}
fmt.Printf("%#v\n", a) Suddenly you need to either implement your custom format string for everything, just so you can get at the time.Time, or hack up something like More to the point this is why the GoStringer interface exists. |
I see your point, you can still easily wrap it but yeah it'd be nice. |
This mostly makes sense, but it would have to hide the monotonic time if any. |
Hi, ping here, I think this would be a very helpful change for Go 1.16, and I would love to work on this if there is interest. |
Change https://golang.org/cl/267017 mentions this issue: |
Hi, now that the 1.17 source tree is open, I would be interested in getting feedback on this proposal. I submitted CL 267017 as an idea for how this feature could be implemented, that could hopefully be used as a jumping off point. |
This seems reasonable (still). Does anyone object to adding this? |
This proposal has been added to the active column of the proposals project |
The stickiest point I think is how to represent a
Of these, I think (3) is best. I don't think it's possible to implement (1) unless I am missing some aspect of the runtime that would make it possible to retrieve a pointer without importing the reflect package. |
GoString doesn't have to give exact Go syntax, I don't think. It prints things like 'main.T', which is not going to compile either. So just saying time.Location("NAME") would probably be fine. |
Based on the discussion above, this proposal seems like a likely accept. |
To be clear, I believe the current proposal is to return strings like:
|
Yes, that's my understanding |
No change in consensus, so accepted. 🎉 |
Great, thanks! I am implementing
I'm not sure how strongly you feel about taking additional dependencies vs. copying code |
I have an array of time.Time objects that I retrieved from the database, and I would like to place them in a Go source file. As I understand it, there's not an easy way to do this without a helper method.
fmt.Sprintf("%#v\n", t)
prints out something like:which not only makes it difficult to determine what time is represented - I can't do math on 63724924180 in my head to determine whether this is yesterday or today, but also can't be embedded directly in a Go program because
wall
andext
andloc
are private variables.I could use
t.Format(time.RFC3339)
on each item to get an array of strings, but then these need to be re-Parse'd to get a time.Time back, with appropriate error handling.It could be nice to have either a
(time.Time) GoString() string
method or aGoString
format constant which would print out a format that could be embedded in a Go source file.For example
could yield
or similar. I am not sure whether having time.Time implement the GoStringer interface - and change the format printed by
%#v
- would violate the Go compatibility promise, though I doubt the currentwall, ext, loc
format is very useful for most people.The trickiest part of this would be printing
Location
, which could be implemented by printing out the pointer address for non-nil, non-UTC, non-Local locations - it's no worse than what's currently done.I suppose we'd also lose some data about the monotonic-ness of the time measurement in question but if you're trying to put this in source code you likely don't care about that bit.
The text was updated successfully, but these errors were encountered: