go - MapScan 无法解码为非指针 int64

标签 go gocql

我使用了 MapScan 并用这个错误对其进行了迭代

can not unmarshal into non-pointer 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
}

最佳答案

我找到了结果:

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
}

关于go - MapScan 无法解码为非指针 int64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51464646/

相关文章:

regex - 如何使用正则表达式提取此子字符串?

windows - 无法创建连接到 Cassandra 的 session

go - 如何使用 gocql 执行 cql 文件?

docker - 如何在Docker Compose多容器应用程序中使用GoCQL连接到bitnami/cassandra?

go - 将 map 作为值传递到 Cassandra 中

go - Golang 中处理 nil 和 error 的惯用方法是什么?

templates - golang 模板中的调用链

go - 为什么这段代码中会出现 fatal error : all goroutines are asleep - deadlock!?

go - 参数和从位置 [1] 传递的参数(不是位置 0)