Go:使用 tx.MustExec 创建新记录后出现恐慌错误
Go: panic error after creating a new record with tx.MustExec
当我使用 db.Exec
时,它工作正常并且也在创建记录,但它会引起恐慌。我不知道为什么。
我是 golang 的初学者,所以我不确定这个错误是什么意思以及如何解决。如果我注释掉 panic 部分,我的整个应用程序都可以正常工作。
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(, now(), now(), NULL, , NULL, , , , 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/httprouter@v1.3.0/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 不是 return 一个错误,而是 return 一个接口
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 := tx.MustExec("INSERT INTO users (id, created_at, updated_at, deleted_at, username, password, first_name, last_name, phone, status) VALUES(, now(), now(), NULL, , NULL, , , , true)", user.ID, user.Username, user.FirstName, user.LastName, user.Phone)
fmt.Println(result.RowsAffected())
当我使用 db.Exec
时,它工作正常并且也在创建记录,但它会引起恐慌。我不知道为什么。
我是 golang 的初学者,所以我不确定这个错误是什么意思以及如何解决。如果我注释掉 panic 部分,我的整个应用程序都可以正常工作。
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(, now(), now(), NULL, , NULL, , , , 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/httprouter@v1.3.0/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 不是 return 一个错误,而是 return 一个接口
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 := tx.MustExec("INSERT INTO users (id, created_at, updated_at, deleted_at, username, password, first_name, last_name, phone, status) VALUES(, now(), now(), NULL, , NULL, , , , true)", user.ID, user.Username, user.FirstName, user.LastName, user.Phone)
fmt.Println(result.RowsAffected())