title: Insert string array into postgres text array field
body: <p>I'm trying to make an article taggable. </p>
  
  <p>Article table:</p>
  
  <pre><code>type Article struct {
    ID int64
    Body string
    Tags string
  }
  </code></pre>
  
  <p>Preparing values:</p>
  
  <pre><code>tags := r.FormValue("tags")
  tagArray := fmt.Sprintf("%q", strings.Split(tags, ", ")) // How do I make use of this?
  
  t := Article{
      Body: "this is a post",
      Tags: `{"apple", "orange"}`,    // I have to hard code this for this to work.
  }
  if err := t.Insert(Db); err != nil {
     // Error handling
  }
  </code></pre>
  
  <p>Database query:</p>
  
  <pre><code>func (t *Article) Insert(db *sqlx.DB) error {
      nstmt, err := db.PrepareNamed(`INSERT INTO articles
      (body, tags)
      VALUES (:body, :tags)
      RETURNING *;
      `)
      if err != nil {
          return err
      }
      defer nstmt.Close()
  
      err = nstmt.QueryRow(t).StructScan(t)
      if err, ok := err.(*pq.Error); ok {
          return err
      }
      return err
  }
  </code></pre>
  
  <p>Postgres setup for tags field:</p>
  
  <pre><code>tags character varying(255)[] DEFAULT '{}',
  </code></pre>
  
  <p>It seems like I have to hard code the value for tags for it to work. Otherwise I would get errors like these:</p>
  
  <pre><code>pq: missing dimension value
  OR
  pq: array value must start with "{" or dimension information
  </code></pre>
  
  <p>How do I make use of <code>tagArray</code>?</p>
  
  <p>Helpful reference: <a href="https://gist.github.com/adharris/4163702" rel="nofollow">https://gist.github.com/adharris/4163702</a></p>