package main
import (
driverMysql "github.com/go-sql-driver/mysql"
"gorm.io/gorm"
gormMysql "gorm.io/driver/mysql"
"time"
)
func main(){
dsnConfig:=&driverMysql.Config{
User: "root",
Addr: "127.0.0.1:3306",
Collation: "utf8mb4_bin",
ParseTime: true,
Loc: time.FixedZone("Asia/HongKong", 8*60*60),
}
db, err := gorm.Open(gormMysql.New(gormMysql.Config{
DSNConfig: dsnConfig,
DontSupportRenameColumn: true,
}))
if err!=nil{
panic(err)
}
_ = db
}
got:
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x5cddd8]
goroutine 1 [running]:
database/sql.(*DB).Close(0x0)
/usr/local/go/src/database/sql/sql.go:883 +0x38
gorm.io/gorm.Open({0x83f1b0?, 0xc000012ba8}, {0x0, 0x0, 0x0})
/Users/a/go/pkg/mod/gorm.io/gorm@v1.25.1/gorm.go:185 +0x7ff
main.main()
xxxx/main.go:20 +0x225
run on ubuntu 2004 with mysql Ver 8.0.32-0ubuntu0.20.04.2 for Linux on x86_64 ((Ubuntu))
go.mod:
require (
github.com/go-sql-driver/mysql v1.7.1
github.com/jinzhu/inflection v1.0.0
github.com/jinzhu/now v1.1.5
gorm.io/driver/mysql v1.5.1
gorm.io/gorm v1.25.1
)
Description
got "nil pointer dereference" when i passing a non-exist timezone name in time.FixedZone in mysql.Config.Loc
It should tell me the timezone name do not exist install of "nil pointer dereference"
Change the "Asia/HongKong" to "Asia/Hong_Kong" will fix the bug.
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: black-06
From the stack info, this is the panic caused by the std lib database/sql.
Comment From: methane
From the stack info, this is the panic caused by the std lib
database/sql.
No! See this code.
https://github.com/go-gorm/gorm/blob/740f2be4535f7156752a5e6ce4bc94db59948a10/gorm.go#L180-L185
db.DB() returns (*sql.DB)nil. And gorm calls ((*sql.DB)nil).Close(). That's why nil pointer dereference happened.
I changed that code like this:
err = config.Dialector.Initialize(db)
if err != nil {
if db, err := db.DB(); err == nil {
fmt.Printf("db=%v\n", db)
if db != nil {
_ = db.Close()
}
Then I got:
db=<nil>
2023/05/31 22:11:42 /Users/inada-n/tmp/gomysql-1441/t.go:17
[error] failed to initialize database, got error unknown time zone Asia/HongKong
panic: unknown time zone Asia/HongKong
goroutine 1 [running]:
main.main()
/Users/inada-n/tmp/gomysql-1441/t.go:22 +0x108
Comment From: black-06
Sry, let's reopen this issue and fix it
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: black-06
From the stack info, this is the panic caused by the std lib
database/sql.No! See this code.
https://github.com/go-gorm/gorm/blob/740f2be4535f7156752a5e6ce4bc94db59948a10/gorm.go#L180-L185
db.DB()returns(*sql.DB)nil. And gorm calls((*sql.DB)nil).Close(). That's why nil pointer dereference happened.I changed that code like this:
```go err = config.Dialector.Initialize(db)
if err != nil { if db, err := db.DB(); err == nil { fmt.Printf("db=%v\n", db) if db != nil { _ = db.Close() }```
Then I got:
``` db=
2023/05/31 22:11:42 /Users/inada-n/tmp/gomysql-1441/t.go:17 [error] failed to initialize database, got error unknown time zone Asia/HongKong panic: unknown time zone Asia/HongKong
goroutine 1 [running]: main.main() /Users/inada-n/tmp/gomysql-1441/t.go:22 +0x108 ```
Thanks for the reminder. I'm a fool 😖
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨