title: How to handle error of singleton only once
body: <h1>How to handle error of singleton only once?</h1>
  <p>I have a singleton service which could generate error only at first call and then it returns already created instance.</p>
  <p>Service looks like below:</p>
  <pre class="lang-golang prettyprint-override"><code>package data
  
  import (
      &quot;sync&quot;
  
      &quot;gorm.io/driver/postgres&quot;
      &quot;gorm.io/gorm&quot;
  )
  
  var (
      databaseSingleton *gorm.DB
      once              sync.Once
  )
  
  func NewDatabase() (*gorm.DB, error) {
      once.Do(func() {
          // ...
          databaseSingleton, err = gorm.Open(postgres.Open(connectionString), config)
          if err != nil {
              return nil, err
          }
      })
      return databaseSingleton, nil
  }
  </code></pre>
  <p>The problem is multiple services which uses <code>databaseSingleton</code> above handle error which can occurs only once.</p>
  <p>Services which uses <code>databaseSingleton</code> looks like below:</p>
  <pre class="lang-golang prettyprint-override"><code>func NewServiceOne() (ServiceOne, error) {
     database, err := NewDatabase()
     // want omit this error handling
     if err != nil {
         return nil, err
     }
     return &amp;serviceOne{database}, nil
  }
  func NewServiceTwo() (ServiceTwo, error) {
     database, err := NewDatabase()
     // want omit this error handling
     if err != nil {
         return nil, err
     }
     return &amp;serviceTwo{database}, nil
  }
  func NewServiceThree() (ServiceThree, error) {
     database, err := NewDatabase()
     // want omit this error handling
     if err != nil {
         return nil, err
     }
     return &amp;serviceThree{database}, nil
  }
  </code></pre>
  <h2>If there any way to omit this error handling because <code>err</code> could be generated only once?</h2>