-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
What version of Go are you using (go version)?
go version go1.7 windows/amd64
What did you do?
I set a variable I was to reuse, var m time.Month, but forgot to set it. When printing everything to check my inputs, fmt.Println panicked.
A trivial replication: https://play.golang.org/p/lGKrw6nBb3
What did you expect to see?
A printed zero value for my month.
What did you see instead?
%!v(PANIC=runtime error: index out of range) 0
Discussion
If one initiates time.Month without setting it to a specific month, it fails to print, because int defaults to zero, but months are expected to be within [1, 12], so the String() method goes out of bounds on the slice of prepared strings.
I don't think the zero value can feasibly be January, because while that would solve the String() issue, comparisons to time.January would not work (since time.January is time.Month(1)). Perhaps prepending the months array with something (not sure if there's a precedent for this) and changing the String() method from func (m Month) String() string { return months[m-1] } to func (m Month) String() string { return months[m] } might be enough.