michas
michas

Reputation: 26555

How to declare a constant date in go

I tried to define a constant date like this:

const fixed = time.Date(2018, time.January, 3, 1, 2, 3, 0, time.UTC)

However this does not work because of const initializer is not a constant. :(

Although I understand that technically Date is a function call, semantically this is a very constant expression.

Is there a way to define a date as a constant?

Upvotes: 11

Views: 6089

Answers (1)

Emil
Emil

Reputation: 2167

Unfortunately, Go doesn't let you define struct constants. What I would suggest you do is to declare const int64 representing your time as a Unix timestamp. Then whenever you need to use it, you can call time.UnixNano.

Upvotes: 10

Related Questions