title: Get error code number from postgres in Go
body: <p>I'm simply unable to retrieve the error code number when I get an error in postgres. </p>
  
  <p>In the test of my program I know I'll get the following error 
  " pq: duplicate key value violates unique constraint "associations_pkey"".</p>
  
  <p>Looking in the postgres docs this is most likely an pq error code of 23505.</p>
  
  <p>I need to get that number in my Go program so that I can check on different types of errors and respond to the end user in a helpful way. </p>
  
  <p>However, I can't seem to get hold of the error code in Go, only the error message. My code is as follows:</p>
  
  <pre><code>stmt, _ := DB.Prepare("INSERT INTO table (column_1) VALUES ($1)")
  
  _, err = stmt.Exec("12324354")
  
  if err != nil {
      log.Println("Failed to stmt .Exec while trying to insert new association")
      log.Println(err.Error())
      fmt.Println(err.Code())
  
  } else {
      Render.JSON(w, 200, "New row was created succesfully")
  }
  </code></pre>