In my RESTFUL web service which is an online game, I'm storing starting time of every question in an global variable like this: var MyTime time.Time
which I should update it after every level of the game. My application is distributed, so I want to make sure all of my apps are not updating it at the same time. That's why I've decided to make it atomic.
Actually I'm familiar with Golang sync/atomic
package.
I tried to use atomic.LoadPointer()
method but it needs specific argument type which isn't safe. Do you any other way for this?
Update:
Okay I solved my problem like this.
I defined time variable as atomic.Value
and used atomic Load and Store methods. This is the code:
var myTime atomic.Value
myTime.Store(newTime)
and load myTime.Load().(time.Time)
.
Consider that Load() method returns interface, so you should write (time.Time) at the end in order to convert it to time.Time type.