我正在嘗試掃描查詢回傳的行,但出現此錯誤
panic: (func() string) 0xc0000ac108
goroutine 1 [running]:
main.main()
C:/Users/User/OneDrive/Dokumente/AIR/go/access/showAccess.go:35 0x445
exit status 2
,我不明白。所有變數的型別都正確,Scan 應該直接轉換型別。這是代碼:
var (
id int
operation string
time string
operator int
)
// query for all records in accesses table
rows, err := DBClient.Query("SELECT * FROM accesses")
if err != nil {
panic(err.Error)
}
defer rows.Close()
// printing each record formatted
for rows.Next() {
if err := rows.Scan(&id, &operation, &time, &operator); err != nil {
panic(err.Error) // here the error comes
}
fmt.Printf("ID: =, operation: s, time: s, operator: %d\n",
id, operation, time, operator)
}
珍惜你的時間。
uj5u.com熱心網友回復:
我想您的列回傳的順序與您在 Scan 呼叫中輸入的順序不同。嘗試洗掉 '*',并放置實際的列名,或將引數重新排序到 Scan 呼叫。
uj5u.com熱心網友回復:
我忘了在 Error 后面加上括號,就像 Yinon Eliraz 提到的那樣。寫作panic(err.Error())解決了問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/388887.html
