Skip to content

time: Zero value of time.Month fails to print #17720

@kokes

Description

@kokes

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions