【发布时间】:2019-07-29 04:50:19
【问题描述】:
在 PostgreSQL 数据库中,我有一个表:
| ORGANIZATION_ID | FACTOR_IDS | CALCULATION_VALUES |
|-----------------|--------------|---------------------|
| 1 | {1,2,3,4,5} | {0,66.66,50,100,80} |
| 2 | NULL | NULL |
| 1 | {6,7,8,9,10} | {0,77.77,60,110,90} |
在 Go 中,我对该表进行查询,然后尝试使用 Scan 方法。不幸的是,我收到一个错误:
Trace: runtime error: invalid memory address or nil pointer dereference
我的代码:
type Entry struct {
OrganizationID int
FactorIDS pq.Int64Array
CalculationValues pq.Float64Array
}
rows, err = database.DBGORM.Raw(`SELECT * FROM ANALYTICS`, ID).Rows()
if err != nil {
utils.Logger().Println(err)
return
}
defer rows.Close()
for rows.Next() {
var entry *Entry
if err = rows.Scan(&entry.OrganizationID, &entry.FactorIDS, &entry.CalculationValues); err != nil {
utils.Logger().Println(err) // <- RAISE ERROR
return
}
if entry.FactorIDS != nil {
for index, value := range factorID {
// some code here
}
}
}
我该如何解决这个问题?
另外,如果我将类型从 pq.Int64Array 更改为 *pq.Int64Array,Go 编译器会给出错误:Cannot range over data *pq.Int64Array 上面的代码。
【问题讨论】:
标签: postgresql go go-gorm