title: Database Connection golang mysql
body: <p>I am trying to write a test program for my Go code. This code has a global <code>db</code> variable which I initialize in the <code>main</code> package.</p>
  
  <pre><code>package database
  
  import(
      "database/sql"
      _ "github.com/go-sql-driver/mysql"
  )
  
  //Data type that defines one identity
  type element struct {
       title string
       date string
       url string
       remoteUrl string
  }
  
  
  //global database object for every package
  var (
     db *sql.DB
  )
  
  // params  elem : element to be inserted ,     folder     : folderName 
  func insertNoticeData( elem element, folder string) bool  {
  
       switch folder {
           case "Results" : stmt, err := db.Prepare("INSERT results_ipu SET title=?, date=?, url=?, remoteUrl=?")
           case "Notices" : stmt, err := db.Prepare("INSERT notice_ipu SET title=?, date=?, url=?, remoteUrl=?")
           case "Datesheets" : stmt, err := db.Prepare("INSERT datesheet_ipu SET title=?, date=?, url=?, remoteUrl=?")
       }
  
       res, err1 := stmt.Exec(elem.title, elem.date, elem.url, elem.remoteUrl)
       if err1 != nil {
       fmt.Println("Error inserting in database ")
       return false
       }
       return true
  }
  </code></pre>
  
  <p>It's giving me an error: <code>undefined symbol stmt</code></p>
  
  <p>What am I missing here?</p>