登录
首页 >  Golang >  Go问答

Go:使用 tx.MustExec 创建新纪录时出现 panic 错误

来源:stackoverflow

时间:2024-02-16 10:57:21 147浏览 收藏

今天golang学习网给大家带来了《Go:使用 tx.MustExec 创建新纪录时出现 panic 错误》,其中涉及到的知识点包括等等,无论你是小白还是老手,都适合看一看哦~有好的建议也欢迎大家在评论留言,若是看完有所收获,也希望大家能多多点赞支持呀!一起加油学习~

问题内容

当我使用 db.exec 时,它工作正常并创建记录,但它引起恐慌。我不知道为什么。 我是 golang 的初学者,所以我不确定这个错误意味着什么以及如何解决。如果我注释掉恐慌部分,我的整个应用程序就可以工作。

tx := db.mustbegin()
// _,err := db.exec(queries.queryinsertuserdata, user.id, user.username, user.firstname, user.lastname, user.phone)
err := tx.mustexec("insert into users (id, created_at, updated_at, deleted_at, username, password, first_name, last_name, phone, status) values($1, now(), now(), null, $2, null, $3, $4, $5, true)", user.id, user.username, user.firstname, user.lastname, user.phone)
tx.commit()

if err != nil {
  panic(err) // giving error
}

错误:

2022/05/28 09:57:17 http: panic serving [::1]:53468: {0xc0001b6000 1}
goroutine 35 [running]:
net/http.(*conn).serve.func1(0xc0001b2280)
        /usr/local/go/src/net/http/server.go:1772 +0x139
panic(0x13538e0, 0xc0000a8780)
        /usr/local/go/src/runtime/panic.go:975 +0x3e3
GO_APP/app/handler.CreateUser(0xc00009aea0, 0x13ff7c0, 0xc00018e1c0, 0xc0001d0000)
        /Users/kritisahu/Desktop/go_app/app/handler/users.go:61 +0x7ec
GO_APP/app.(*App).CreateUser(...)
        /Users/kritisahu/Desktop/go_app/app/app.go:47
github.com/julienschmidt/httprouter.(*Router).ServeHTTP(0xc000182420, 0x13ff7c0, 0xc00018e1c0, 0xc0001d0000)
        /Users/kritisahu/go/pkg/mod/github.com/julienschmidt/[email protected]/router.go:387 +0xc37
net/http.serverHandler.ServeHTTP(0xc00018e0e0, 0x13ff7c0, 0xc00018e1c0, 0xc0001d0000)
        /usr/local/go/src/net/http/server.go:2807 +0xa3
net/http.(*conn).serve(0xc0001b2280, 0x14000c0, 0xc0001ba040)
        /usr/local/go/src/net/http/server.go:1895 +0x86c
created by net/http.(*Server).Serve
        /usr/local/go/src/net/http/server.go:2933 +0x35c

正确答案


您收到错误,因为 Tx.MustExec 不返回错误,但返回接口

type result interface {
    // lastinsertid returns the integer generated by the database
    // in response to a command. typically this will be from an
    // "auto increment" column when inserting a new row. not all
    // databases support this feature, and the syntax of such
    // statements varies.
    lastinsertid() (int64, error)

    // rowsaffected returns the number of rows affected by an
    // update, insert, or delete. not every database or database
    // driver may support this.
    rowsaffected() (int64, error)
}

因此在您的代码中 err 存储 result 接口值。而不是像这样写错误

result := tx.MustExec("INSERT INTO users (id, created_at, updated_at, deleted_at, username, password, first_name, last_name, phone, status) VALUES($1, now(), now(), NULL, $2, NULL, $3, $4, $5, true)", user.ID, user.Username, user.FirstName, user.LastName, user.Phone)

fmt.Println(result.RowsAffected())

理论要掌握,实操不能落!以上关于《Go:使用 tx.MustExec 创建新纪录时出现 panic 错误》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>