title: "Perfect singleton" via using sync.Once?
body: <p>i am confusing that, is below snippet perfect?</p>
  
  <pre><code>import "sync"
  import "sync/atomic"
  
  var initialized uint32
  var instance *singleton
  
  var instance *singleton
  var once sync.Once
  
  func GetInstance() *singleton {
      once.Do(func() {
          instance = &amp;singleton{}
      })
      return instance
  }
  </code></pre>
  
  <p><code>atomic.StoreUint32(&amp;initialized, 1)</code> will flush instance to all CPUs?
  i think i need add an atomic store and load for instance, like below snippet</p>
  
  <pre><code>var instance *singleton
  var once sync.Once
  
  func GetInstance() *singleton {
      once.Do(func() {
          atomic.StorePointer(&amp;instance, &amp;singleton{})
      })
      return atomic.LoadPointer(&amp;instance)
  }
  </code></pre>
  
  <p>i think Once.Do is only guarantee execute function f one time.
  and <code>atomic.StoreUint32(&amp;o.done, 1)</code> is only memory barrier for o.done. 
  it doesn't ensure <code>instance</code> is global visible</p>
  
  <pre><code>func (o *Once) Do(f func()) {
      if atomic.LoadUint32(&amp;o.done) == 1 {
          return
      }
      // Slow-path.
      o.m.Lock()
      defer o.m.Unlock()
      if o.done == 0 {
          defer atomic.StoreUint32(&amp;o.done, 1)
          f()
      }
  }
  </code></pre>