我有 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
現在您實際上已經為Title
和Description
字段分配了空間,因此有地方可以存儲這些值。
- 1 回答
- 0 關注
- 151 瀏覽
添加回答
舉報
0/150
提交
取消