title: Golang code running really slow than same code in PHP
body: <p>New to Golang, Yesterday I've started to play with Golang and wrote some code which was actually written in PHP. I just wanted to see difference in performance.</p>
  <p>I am doing the exact same thing in PHP response is exact same in http request but the Golang is performing really slow even after compiling it.</p>
  <p>I am trying to understand what things that I am using in Golang I shouldn't be using and how can I improve performance in this piece of Code.</p>
  <p>I know Iterating over map is slow but PHP using hash maps for implementing multidimentional arrays, well. I can gurantee the sql queries I used were exact same copy pasted from PHP, machines are same, and loop numbers are same in both codes.</p>
  <pre><code>package main
  
  import (
      &quot;database/sql&quot;
      &quot;encoding/json&quot;
      &quot;fmt&quot;
      _ &quot;github.com/go-sql-driver/mysql&quot;
      &quot;net/http&quot;
      &quot;reflect&quot;
      &quot;strings&quot;
  )
  
  func main() {
      db, err := sql.Open(&quot;mysql&quot;, &quot;***:****@tcp(****:3306)/****&quot;)
      fmt.Println(reflect.TypeOf(db))
      checkErr(err)
      fmt.Println(&quot;Handle Request setup... OK&quot;)
      http.HandleFunc(&quot;/&quot;, func(w http.ResponseWriter, req *http.Request) {
  
          jsonData, err := getListings(db)
          checkErr(err)
  
          w.Write([]byte(jsonData))
  
      })
      fmt.Println(&quot;Starting Server....&quot;)
      fmt.Println(&quot;Listening on port 8081&quot;)
      http.ListenAndServe(&quot;:8081&quot;, nil)
  
  }
  
  func getListings(db *sql.DB) ([]byte, error) {
      var userId string = &quot;142&quot;
  
      normalListings := sqlToArray(db, `******`)
  
      manualListings := sqlToArray(db, &quot;******&quot;)
  
      var groupIds []string
      for key := range manualListings {
  
          groupId := &quot;142,&quot; + manualListings[key][&quot;group_id&quot;]
          if !stringInSlice(groupId, groupIds) {
              groupIds = append(groupIds, groupId)
          }
      }
  
      var groupIdsString string
      groupIdsString = strings.Join(groupIds, &quot;, &quot;)
  
      manualGroups := sqlToArray(db, &quot;*****&quot;)
  
      for key := range manualListings {
  
          for key2 := range manualGroups {
              groupId := &quot;142,&quot; + manualListings[key][&quot;group_id&quot;]
  
              if groupId == manualGroups[key][&quot;ticket_id&quot;] {
                  entry := make(map[string]string)
                  entry[&quot;ticket_id&quot;] = manualListings[key][&quot;listing_id&quot;]
                  entry[&quot;date_created&quot;] = manualGroups[key2][&quot;date_created&quot;]
                  normalListings = append(normalListings, entry)
  
              }
          }
      }
  
      return json.Marshal(normalListings)
  
  }
  
  func stringInSlice(a string, list []string) bool {
      for _, b := range list {
          if b == a {
              return true
          }
      }
      return false
  }
  
  func sqlToArray(db *sql.DB, sqlString string) []map[string]string {
  
      rows, err := db.Query(sqlString)
      checkErr(err)
      columns, err := rows.Columns()
      count := len(columns)
      values := make([]interface{}, count)
      valuePtrs := make([]interface{}, count)
      tableData := make([]map[string]string, 0)
  
      for rows.Next() {
  
          for i := 0; i &lt; count; i++ {
              valuePtrs[i] = &amp;values[i]
          }
          rows.Scan(valuePtrs...)
          entry := make(map[string]string)
          for i, col := range columns {
  
              val := values[i]
              b, ok := val.([]byte)
              if ok {
                  entry[col] = string(b)
              } else {
                  entry[col] = string(b)
              }
  
          }
          tableData = append(tableData, entry)
  
      }
  
      return tableData
  
  }
  
  func checkErr(err error) {
      if err != nil {
          panic(err)
      }
  }
  </code></pre>
  <p><strong>Edits:</strong>
  Changed the code to use statically typed structs instead of using maps and Identified the problematic piece of code</p>
  <p><strong>New code:</strong></p>
  <pre><code>package main
  
  import (
      &quot;database/sql&quot;
      &quot;encoding/json&quot;
      &quot;fmt&quot;
      _ &quot;github.com/go-sql-driver/mysql&quot;
      &quot;net/http&quot;
      &quot;strings&quot;
  )
  
  type listingsType struct {
      TicketId    string
      DateCreated string
  }
  
  func main() {
      db, err := sql.Open(&quot;mysql&quot;, &quot;******&quot;)
  
      checkErr(err)
      fmt.Println(&quot;Handle Request setup... OK&quot;)
      http.HandleFunc(&quot;/&quot;, func(w http.ResponseWriter, req *http.Request) {
  
          jsonData, err := getListings(db)
          checkErr(err)
  
          w.Write([]byte(jsonData))
  
      })
      fmt.Println(&quot;Starting Server....&quot;)
      fmt.Println(&quot;Listening on port 8081&quot;)
      http.ListenAndServe(&quot;:8081&quot;, nil)
  
  }
  
  func getListings(db *sql.DB) ([]byte, error) {
      var userId string = &quot;142&quot;
  
      normalListings := sqlToArray(db, `*****`)
  
      manualListings := sqlToArray(db, &quot;*****&quot;)
  
      var groupIds []string
  
      for _, elem := range manualListings {
          groupId := &quot;142,&quot; + elem.DateCreated
          if !stringInSlice(groupId, groupIds) {
              groupIds = append(groupIds, groupId)
          }
  
      }
  
      var groupIdsString string
      groupIdsString = strings.Join(groupIds, &quot;, &quot;)
      fmt.Println(groupIdsString)
      manualGroups := sqlToArray(db, &quot;******&quot;)
  
      for _, manualList := range manualListings {
  
          for _, manualGroup := range manualGroups {
              groupId := &quot;142,&quot; + manualList.DateCreated
  
              if groupId == manualGroup.TicketId {
                  var entry listingsType
                  entry.TicketId = manualList.TicketId
                  entry.DateCreated = manualGroup.DateCreated
                  normalListings = append(normalListings, entry)
  
              }
          }
      }
  
      return json.Marshal(normalListings)
  
  }
  
  func stringInSlice(a string, list []string) bool {
      for _, b := range list {
          if b == a {
              return true
          }
      }
      return false
  }
  
  func sqlToArray(db *sql.DB, sqlString string) []listingsType {
  
      rows, err := db.Query(sqlString)
      checkErr(err)
  
      tableData := []listingsType{}
  
      for rows.Next() {
  
          var entry listingsType
          rows.Scan(&amp;entry.TicketId, &amp;entry.DateCreated)
  
          tableData = append(tableData, entry)
  
      }
  
      return tableData
  
  }
  
  func checkErr(err error) {
      if err != nil {
          panic(err)
      }
  }
  </code></pre>
  <p><strong>Problematic piece of code</strong>
  As soon as I comment the following block of code the my code performs just fine.</p>
  <p>Any idea what is wrong with this loop ?</p>
  <pre><code>for _, manualList := range manualListings {
  
          for _, manualGroup := range manualGroups {
              groupId := &quot;142,&quot; + manualList.DateCreated
  
              if groupId == manualGroup.TicketId {
                  var entry listingsType
                  entry.TicketId = manualList.TicketId
                  entry.DateCreated = manualGroup.DateCreated
                  normalListings = append(normalListings, entry)
  
              }
          }
      }
  </code></pre>
  <p><strong>Profiling Result</strong></p>
  <p><a href="https://i.stack.imgur.com/Kzq7F.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Kzq7F.png" alt="enter image description here" /></a></p>