登录
首页 >  Golang >  Go问答

无法将 MapScan 解组为非指针的 int64 值

来源:stackoverflow

时间:2024-03-23 18:27:30 309浏览 收藏

在使用 MapScan 时出现 "无法解组为非指针 int64" 错误,表明在迭代过程中无法正确解析 int64 数据类型。为了解决此问题,需要在 for 循环中添加额外的代码,重置 rowValues 映射,以确保每次迭代都能正确解析数据。

问题内容

我使用 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
}

解决方案


我找到了结果:

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

这是最终代码:

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
}

终于介绍完啦!小伙伴们,这篇关于《无法将 MapScan 解组为非指针的 int64 值》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>