title: panic: runtime error: invalid memory address or nil pointer dereference with bigger data
body: <p>I am working on a recommendation engine with Apache Prediction IO. Before the event server i have an GO api that listens events from customer and importer. In a particular case when customer uses importer i collect the imported identitys and i send in a json from importer api to GO api. As an example if user imports a csv that contains 45000 data, i send those 45000 identity to  GO api in a json like <code>{&quot;barcodes&quot;:[...]}</code>. Prediction IO event server wants data in a particular shape.</p>
  <pre><code>type ItemEvent struct {
      Event      string              `json:&quot;event&quot;`
      EntityType string              `json:&quot;entityType&quot;`
      EntityId   string              `json:&quot;entityId&quot;`
      Properties map[string][]string `json:&quot;properties&quot;`
      EventTime  time.Time           `json:&quot;eventTime&quot;`
  }
  
  
  type ItemBulkEvent struct {
      Event     string    `json:&quot;event&quot;`
      Barcodes []string  `json:&quot;barcodes&quot;`
      EventTime time.Time `json:&quot;eventTime&quot;`
  }
  </code></pre>
  <p><code>ItemEvent</code> is the final data that i will send to event server from GO Api. <code>ItemBulkEvent</code> is the data that i receive from importer api.</p>
  <pre><code>func HandleItemBulkEvent(w http.ResponseWriter, r *http.Request) {
      var itemBulk model.ItemBulkEvent
      err := decode(r,&amp;itemBulk)
      if err != nil {
          log.Fatalln(&quot;handleitembulkevent -&gt; &quot;,err)
          util.RespondWithError(w,400,err.Error())
      }else {
          var item model.ItemEvent
          item.EventTime = itemBulk.EventTime; item.EntityType = &quot;item&quot;; item.Event = itemBulk.Event
          itemList := make([]model.ItemEvent,0,50)
          for index, barcode := range itemBulk.Barcodes{
              item.EntityId = barcode
              if (index &gt; 0 &amp;&amp; (index % 49) == 0){
                  itemList = append(itemList, item)
                  go sendBulkItemToEventServer(w,r,itemList)
                  itemList = itemList[:0]
              }else if index == len(itemBulk.Barcodes) - 1{
                  itemList = append(itemList, item)
                  itemList = itemList[:( (len(itemBulk.Barcodes) - 1) % 49)]
                  go sendBulkItemToEventServer(w,r,itemList) // line 116 
                  itemList = itemList[:0]
              } else{
                  itemList = append(itemList, item)
              }
          }
          util.RespondWithJSON(w,200,&quot;OK&quot;)
      }
  }
  </code></pre>
  <p><code>HandleItemBulkEvent</code> is a handler function for bulk updates. In this step i should mention about prediction io's batch uploads. Via rest api prediction io event server takes 50 event per request. So i created a list with 50 cap and an item. I used same item and just changed identity part(barcode) in every turn and added to list. In every 50. item i used a handler function that sends that list to event server and after that cleaned the list so on.</p>
  <pre><code>func sendBulkItemToEventServer(w http.ResponseWriter, r *http.Request, itemList []model.ItemEvent){
      jsonedItem,err := json.Marshal(itemList)
      if err != nil{
          log.Fatalln(&quot;err marshalling -&gt; &quot;,err.Error())
      }
      // todo: change url to event server url
      resp, err2 := http.Post(fmt.Sprintf(&quot;http://localhost:7070/batch/events.json?accessKey=%s&quot;,
          r.Header.Get(&quot;Authorization&quot;)),
          &quot;application/json&quot;,
          bytes.NewBuffer(jsonedItem))
      if err2 != nil{
          log.Fatalln(&quot;err http -&gt; &quot; , err.Error()) // line 141
      }
      defer resp.Body.Close()
  }
  </code></pre>
  <p><code>sendBulkItemToEventServer</code> function marshals the incoming itemlist and makes an post request to prediction io's event server. In this part when i try with 5000+- item it does well but when i try with 45000 item application crashes with below error.</p>
  <pre><code>panic: runtime error: invalid memory address or nil pointer dereference
  [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xc05938]
  
  goroutine 620 [running]:
  api-test/service.sendBulkItemToEventServer(0x1187860, 0xc00028e0e0, 0xc00029c200, 0xc00011c000, 0x31, 0x32)
          /home/kadirakinkorkunc/Desktop/playground/recommendation-engine/pio_api/service/CollectorService.go:141 +0x468
  created by api-test/service.HandleItemBulkEvent
          /home/kadirakinkorkunc/Desktop/playground/recommendation-engine/pio_api/service/CollectorService.go:116 +0x681
  
  Debugger finished with exit code 0
  
  </code></pre>
  <p>Any idea how can i solve this problem?</p>
  <p><strong>edit</strong>: as Burak Serdar mentioned in the answers, i fixed the err, err2 confusion and the data race problem by using marshalling before send. Now it gives me the real error(res,err2) i guess.</p>
  <pre><code>2020/08/03 15:11:55 err http -&gt;  Post &quot;http://localhost:7070/batch/events.json?accessKey=FJbGODbGzxD-CoLTdwTN2vwsuEEBJEZc4efrSUc6ekV1qUYAWPu5anDTyMGDoNq1&quot;: read tcp 127.0.0.1:54476-&gt;127.0.0.1:7070: read: connection reset by peer
  </code></pre>
  <p>Any idea on this?</p>