0

我使用 MapScan 并使用此错误对其进行迭代

无法解组为非指针 int64

第一次迭代后出错。

这是我正在处理的代码:

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
}
4

1 回答 1

1

我找到了结果:

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

我也必须在 for loop 中添加这一行。

这是最终代码:

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
}
于 2018-07-22T11:50:46.923 回答