如何在Go中将time.Time变量转换为atomic?

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.

This can't be done, as such, because time.Time is a compound type:

type Time struct {
    // wall and ext encode the wall time seconds, wall time nanoseconds,
    // and optional monotonic clock reading in nanoseconds.
    //
    // From high to low bit position, wall encodes a 1-bit flag (hasMonotonic),
    // a 33-bit seconds field, and a 30-bit wall time nanoseconds field.
    // The nanoseconds field is in the range [0, 999999999].
    // If the hasMonotonic bit is 0, then the 33-bit field must be zero
    // and the full signed 64-bit wall seconds since Jan 1 year 1 is stored in ext.
    // If the hasMonotonic bit is 1, then the 33-bit field holds a 33-bit
    // unsigned wall seconds since Jan 1 year 1885, and ext holds a
    // signed 64-bit monotonic clock reading, nanoseconds since process start.
    wall uint64
    ext  int64

    // loc specifies the Location that should be used to
    // determine the minute, hour, month, day, and year
    // that correspond to this Time.
    // The nil location means UTC.
    // All UTC times are represented with loc==nil, never loc==&utcLoc.
    loc *Location
}

However, you can do this with pointers, so *time.Time would be possible, if this suits your needs. But of course, this is discouraged, by virtue of the fact that atomic.LoadPointer and atomic.StorePointer use the unsafe package to accomplish their magic.

A much better approach, if it will work for you, is just to use a mutex to protect your value. There are many ways to do this, but one minimal example:

type MyTime struct {
    t  time.Time
    mu sync.RWMutex
}

func (t *MyTime) Time() time.Time {
    t.mu.RLock()
    defer t.mu.RUnlock()
    return t.t
}

func (t *MyTime) SetTime(tm time.Time) {
    t.mu.Lock()
    defer t.mu.Unlock()
    t.t = tm
}

You can keep unix time https://golang.org/pkg/time/#example_Time_Unix as atomic which is int64. Then convert to go time after you've read atomic value.