Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign pointer to variable in Golang

Tags:

go

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?

like image 535
Sredny M Casanova Avatar asked Aug 11 '26 05:08

Sredny M Casanova


1 Answers

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

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

like image 117
Flimzy Avatar answered Aug 13 '26 14:08

Flimzy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!