Sredny M Casanova
Sredny M Casanova

Reputation: 5053

Assign pointer to variable in Golang

I am working in Golang, I have a Struct that contains some fields, one of them is a time.Time field to save the Delete_Atso it can be null, basically I have defined it as:

type Contact struct {
     Delete_At *time.Time
}

So, with the pointer it can be null. Then, I have a method when this value is assigned, I have something like:

contact := Contact{}
contact.Deleted_At = time.Now() 

But with it, I get:

cannot use time.Now() (type time.Time) as type *time.Time in assignment

I totally understand that is a bad assignment, but, how should I do it? how the conversion should be done?

Upvotes: 3

Views: 5000

Answers (1)

Jonathan Hall
Jonathan Hall

Reputation: 79546

t := time.Now()
contact.Deleted_At = &t

And BTW, you should not use _ in variable names. DeletedAt would be the recommended name.

Upvotes: 7

Related Questions