亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當我嘗試使用 db.QueryRow 時出現 golang 運行時錯誤

當我嘗試使用 db.QueryRow 時出現 golang 運行時錯誤

Go
Cats萌萌 2023-02-14 17:44:25
我有 controller.go:package controllerimport (    "github.com/fishkaoff/todoList/pkg/postgresql"    "github.com/gin-gonic/gin"    "net/http"    "context")type Task struct {    Title string     Description string }func GetTasks(c *gin.Context) {    var response *Task    err := db.Conn.QueryRow(context.Background(), "select * from tasks").Scan(&response.Title,     &response.Description)    if err != nil {        c.JSON(http.StatusInternalServerError, gin.H{"error":"error"})    }    c.JSON(200, gin.H{        "page":"gettasks",        "tasks": response,    })}當我發送請求時它返回我```runtime error: invalid memory address or nil pointer dereferenceC:/Program Files/Go/src/runtime/panic.go:260 (0xb7bab5)        panicmem: panic(memoryError)C:/Program Files/Go/src/runtime/signal_windows.go:255 (0xb7ba85)        sigpanic: panicmem()D:/workspace/golangWWW/pkg/mod/github.com/jackc/pgx/[email protected]/conn.go:551 (0x1015db1)        (*Conn).Query: simpleProtocol := c.config.PreferSimpleProtocolD:/workspace/golangWWW/pkg/mod/github.com/jackc/pgx/[email protected]/conn.go:663 (0x101b9dc)        (*Conn).QueryRow: rows, _ := c.Query(ctx, sql, args...)D:/workspace/golangWWW/todolist/internal/controller/controller.go:17 (0x101b9ab)        GetTasks: err := db.Conn.QueryRow(context.Background(), "select * from tasks").Scan(&response.Title, &response.Description)D:/workspace/golangWWW/pkg/mod/github.com/gin-gonic/[email protected]/context.go:173 (0xeb99c1)        (*Context).Next: c.handlers[c.index](c)D:/workspace/golangWWW/pkg/mod/github.com/gin-gonic/[email protected]/recovery.go:101 (0xeb99ac)        CustomRecoveryWithWriter.func1: c.Next()
查看完整描述

1 回答

?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

    var response *Task

您已分配內存來存儲 a *Task,而不是 a Task

那么當你

    .Scan(&response.Title,     &response.Description)

嘗試獲取response.Title和的地址response.Description。這些字段沒有地址,因為它response是一個指針但未指向有效任務。

嘗試:

var response Task

現在您實際上已經為TitleDescription字段分配了空間,因此有地方可以存儲這些值。


查看完整回答
反對 回復 2023-02-14
  • 1 回答
  • 0 關注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號