Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
GitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
time: Zero value of time.Month fails to print #17720
What version of Go are you using (
I do not think changing the array and using months[m] is general enough and a good solution since month=99 will return a similar error. If we change behaviour we should make sure all invalid values are handled.
Open question would be what an invalid month (including the zero value) should print instead. If there is a better agreed answer than a printing a runtime error then we can change the String method to print that for invalid values. Also should take into account that month.String() is called directly from other methods in time and those just truncate the returned string. So a panic there might be better preferable over some truncated invalid string that produces confusing output. Code calling String() might also depend on getting a panic for invalid values.
The same question can be asked for the weekdays String method too.
Good point. Shall we perhaps use
I do not think Stringer helps or does make the code better. I rather keep the string array. It is more readable and simpler at least to me. I think the issue is not with the string array as such but what to print or do when the value String should print is outside the valid range.
We can then do something like:
Your proposed solution for ? seems to be to return fmt.Sprintf("Month(%d)", i+1).
However as outlined above this will print "Mon" when truncated by methods that call String() and just truncate the returned string s[:3]. Which can even be confused for Monday in a date format.
Simplifies https://golang.org/cl/33145 which fixed #17720. Change-Id: Ib922d493cdc5920832dc95b55094796baca7243e Reviewed-on: https://go-review.googlesource.com/33194 Reviewed-by: Brad Fitzpatrick <firstname.lastname@example.org> Run-TryBot: Brad Fitzpatrick <email@example.com> TryBot-Result: Gobot Gobot <firstname.lastname@example.org>