我有 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/v4@v4.17.2/conn.go:551 (0x1015db1) (*Conn).Query: simpleProtocol := c.config.PreferSimpleProtocolD:/workspace/golangWWW/pkg/mod/github.com/jackc/pgx/v4@v4.17.2/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/gin@v1.8.1/context.go:173 (0xeb99c1) (*Context).Next: c.handlers[c.index](c)D:/workspace/golangWWW/pkg/mod/github.com/gin-gonic/gin@v1.8.1/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 关注
- 172 浏览
添加回答
举报
0/150
提交
取消