title: How to use global var across files in a package?
body: <p>I have the following file structure:</p>
  
  <p><strong>models/db.go</strong></p>
  
  <pre><code>type DB struct {
      *sql.DB
  }
  
  var db *DB
  
  func init() {
      dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable",
          DB_USER, DB_PASSWORD, DB_NAME)
  
      db, err := NewDB(dbinfo)
      checkErr(err)
  
      rows, err := db.Query("SELECT * FROM profile")
      checkErr(err)
  
      fmt.Println(rows)
  }
  
  func NewDB(dataSourceName string) (*DB, error) {
      db, err := sql.Open("postgres", dataSourceName)
      if err != nil {
          return nil, err
      }
      if err = db.Ping(); err != nil {
          return nil, err
      }
      return &amp;DB{db}, nil
  }
  </code></pre>
  
  <p><strong>models/db_util.go</strong></p>
  
  <pre><code>func (p *Profile) InsertProfile() {
      if db != nil {
          _, err := db.Exec(...)
          checkErr(err)
      } else {
          fmt.Println("DB object is NULL")
      }
  }
  </code></pre>
  
  <p>When I try to access <code>db</code> in <code>InsertProfile</code> function, it says <code>NULL ptr exception</code>. How do I access the <code>db</code> in <code>db_utils.go</code>?</p>
  
  <p>I would not like to capitalize <code>db</code> (as it would give access to all the packages).</p>
  
  <p>I am getting the QUERY returned from the <code>db</code> in <code>init()</code> correctly.</p>