Reputation: 1844
I'm using time.Time as a pointer in one of my structs. Eg
type struct Example{
CreatedDate *time.Time
}
I'm using the pointer, so I can pass nil to the struct, if no date exists.
This does however pose a problem now, since I need to use the time.Since(then) function which does not accept the pointer as the parameter, but takes a time.Time instead.
So...
It's quite easy to put "&" in front of a struct eg. &time.Time, but if you have a pointer, how can you reverse it, back into eg. a type of time.Time?
Eg. (does not work, but might give you an idea of what I mean)
var t *time.Time = &time.Now()
var t2 time.Time = t.(time.Time)
I hope you can help. The question feels quite silly, since I can't find anything about it when googling. I get the feeling I'm just missing something here ;-)
Upvotes: 54
Views: 56076
Reputation: 49187
var t *time.Time = &time.Now()
var t2 time.Time = *t
Just like in C, &
means "address of" and assigns a pointer which is marked as *T
. But *
also means "value of", or dereferencing the pointer, and assigns the value.
This is a bit off-topic, but IMO this dual use of *
is partly what gets newbies in C and friends confused about pointers.
Upvotes: 104