遇到 golang 运行时错误,当尝试使用 db.QueryRow
时间:2024-03-12
浏览:397
作者:佚名
学习Golang要努力,但是不要急!今天的这篇文章《遇到 golang 运行时错误,当尝试使用 db.QueryRow 》将会介绍到等等知识点,如果你想深入学习Golang,可以关注我!我会持续更新相关文章的,希望对大家都能有所帮助!
问题内容
我有controller.go:
package controller
import (
"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 dereference
c:/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.prefersimpleprotocol
d:/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()
d:/workspace/golangwww/pkg/mod/github.com/gin-gonic/[email protected]/context.go:173 (0xeb8ac6)
(*context).next: c.handlers[c.index](c)
d:/workspace/golangwww/pkg/mod/github.com/gin-gonic/[email protected]/logger.go:240 (0xeb8aa9)
loggerwithconfig.func1: c.next()
d:/workspace/golangwww/pkg/mod/github.com/gin-gonic/[email protected]/context.go:173 (0xeb7bd0)
(*context).next: c.handlers[c.index](c)
d:/workspace/golangwww/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:616 (0xeb7838)
(*engine).handlehttprequest: c.next()
d:/workspace/golangwww/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:572 (0xeb74fc)
(*engine).servehttp: engine.handlehttprequest(c)
c:/program files/go/src/net/http/server.go:2947 (0xd70feb)
serverhandler.servehttp: handler.servehttp(rw, req)
c:/program files/go/src/net/http/server.go:1991 (0xd6d6c6)
(*conn).serve: serverhandler{c.server}.servehttp(w, w.req)
c:/program files/go/src/runtime/asm_amd64.s:1594 (0xb96320)
goexit: byte $0x90 // nop```
db.conn 是从 db.go 导入的:
package db
import (
"os"
"fmt"
"context"
"github.com/jackc/pgx/v4/"
"github.com/fishkaoff/todoList/internal/config"
)
var Conn *pgx.Conn
func InitDatabase(config config.Config) {
Conn, err := pgx.Connect(context.Background(), config.DBUrl)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
defer Conn.Close()
}
initdatabase 正在 main.go 中调用
在数据库中创建表和列
我正在使用 pgx v4
p.s:抱歉我的英语不好,我不是母语人士 ____________________________________________________________
正确答案
var response *task
您已分配内存来存储 *task,而不是 task。
然后当你
.scan(&response.title, &response.description)
尝试获取 response.title 和 response.description 的地址。这些字段没有地址,因为 response 是一个指针,但不指向有效的任务。
尝试:
var response Task
现在您实际上已经为 title 和 description 字段分配了空间,因此有地方可以存储这些值。
文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《遇到 golang 运行时错误,当尝试使用 db.QueryRow 》文章吧,也可关注golang学习网公众号了解相关技术文章。
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
热门专题
