0

I used MapScan and iterate on it with this error

can not unmarshal into non-pointer int64

Errors out after the first iteration.

This is the code I was working on:

type NotFinishedTBLFields struct {
    Bulk_id       int64
    Recipient     string
    Operator      string
    Tracking_code string
} 

func FetchNotFinishedTBLRows() *NotFinishedTBLFields {

rowValues := make(map[string]interface{})
var row NotFinishedTBLFields

iter := Instance().Query(fmt.Sprintf(`SELECT * FROM %q `, NotFinishedTBLConfig.Name)).Consistency(gocql.All).Iter()

for iter.MapScan(rowValues) {
    fmt.Println("rowValues ",rowValues)
    row = NotFinishedTBLFields{
        Bulk_id:       rowValues["bulk_id"].(int64),
        Recipient:     rowValues["recipient"].(string),
        Operator:      rowValues["operator"].(string),
        Tracking_code: rowValues["tracking_code"].(string),
    }

}
if err := iter.Close(); err != nil {
    log.Fatal(err)
}
return &row
}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
elham anari
  • 518
  • 2
  • 8
  • 22

1 Answers1

1

I found the result :

rowValues = make(map[string]interface{})

I must add this line in for loop , too.

This is final code :

func FetchNotFinishedTBLRows(limit int) []NotFinishedTBLFields {

rowValues := make(map[string]interface{})
var row NotFinishedTBLFields
var rows []NotFinishedTBLFields
iter := Instance().Query(fmt.Sprintf(`SELECT * FROM %q Limit %d`, NotFinishedTBLConfig.Name, limit)).Consistency(gocql.All).Iter()

for iter.MapScan(rowValues) {
    fmt.Println("rowValues ", rowValues)
    row = NotFinishedTBLFields{
        Bulk_id:       rowValues["bulk_id"].(int64),
        Recipient:     rowValues["recipient"].(string),
        Operator:      rowValues["operator"].(string),
        Tracking_code: rowValues["tracking_code"].(string),
    }
    rows = append(rows, row)
    rowValues = make(map[string]interface{})
}
if err := iter.Close(); err != nil {
    log.Fatal(err)
}
return rows
}
elham anari
  • 518
  • 2
  • 8
  • 22
  • > Each call to MapScan() must be called with a new map object. Take a look at comment in `func (iter *Iter) MapScan(m map[string]interface{}) bool` so exactly what you discovered yourself – jasiustasiu Nov 26 '21 at 16:14