7

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?

michas
  • 25,361
  • 15
  • 76
  • 121
  • 2
    How is this question a duplicate? The linked question discussed constant arrays, this one is asking for a constant date. – Trent Sep 09 '19 at 07:52

1 Answers1

5

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.

Emil
  • 2,098
  • 11
  • 25