title: Reading database rows into map and appending to a slice of maps
body: <p>I am trying to use maps and slice of those maps to store rows returned from a database query. But what I get in every iteration of the rows.Next() and in final is the slice of the one same row from the query. It seems the problem is related to the memory place being same I store the <code>cols</code>, yet I could not resolve it until now.</p>
  
  <p>What is the thing am I missing here:</p>
  
  <p>The source code is as follows:</p>
  
  <pre><code>package main
  
  import (
      "database/sql"
      _ "github.com/lib/pq"
  
      "fmt"
      "log"
  
      "reflect"
  )
  var myMap = make(map[string]interface{})
  var mySlice = make([]map[string]interface{}, 0)
  
  func main(){
      fmt.Println("this is my go playground.")
  
      // DB Connection-
      db, err := sql.Open("postgres", "user=postgres dbname=proj2-dbcruddb-dev password=12345 sslmode=disable")
      if err != nil {
          log.Fatalln(err)
      }
      rows, err := db.Query("SELECT id, username, password FROM userstable")
      defer rows.Close()
      if err != nil {
          log.Fatal(err)
      }
  
      colNames, err := rows.Columns()
      if err != nil {
          log.Fatal(err)
      }
      cols := make([]interface{}, len(colNames))
      colPtrs := make([]interface{}, len(colNames))
      for i := 0; i &lt; len(colNames); i++ {
          colPtrs[i] = &amp;cols[i]
      }
  
      for rows.Next() {
          err = rows.Scan(colPtrs...)
          if err != nil {
              log.Fatal(err)
          }
          fmt.Println("cols: ", cols)
  
          for i, col := range cols {
              myMap[colNames[i]] = col
          }
          mySlice = append(mySlice, myMap)
  
          // Do something with the map
          for key, val := range myMap {
              fmt.Println("Key:", key, "Value:", val, "Value Type:", reflect.TypeOf(val))
          }
          fmt.Println("myMap: ", myMap)
          fmt.Println("mySlice: ", mySlice)
  
      }
      fmt.Println(mySlice)
  }
  </code></pre>