I’m trying to implement singleton while using database.
main.go
db := dbcon.Singleton()
err := db.Ping() // panic here
if err != nil {
panic(err)
}
fmt.Println("Successfully connected!")
in package dbcon I implement singleton this way:
var db *sql.DB // singleton
var once sync.Once
func Singleton() *sql.DB {
once.Do(func() {
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
defer db.Close()
})
return db
}
But I got in main.go
panic: runtime error: invalid memory address or nil pointer dereference
What is wrong with this implementation?
2 Likes
geosoft1
(George Calianu)
August 22, 2019, 5:57pm
2
cinematik:
defer db.Close()
Is because of defer db.Close()
. You try to use the database after you closed it.
3 Likes
If I comment deffer nothing changed. But I’m able successfully ping from inside Singleton() , but in main it is still paniking.
2 Likes
As I found out it was beccause I’ve not initialize var db which is outside of the Singleton() and after I changed name inside and then reassigned it to db it works but I’m not sure is it good solution.
2 Likes
iegomez
(Ignacio Gómez)
August 22, 2019, 6:19pm
5
You were redeclaring db
inside the function called at once.Do
, shadowing the global db
variable and thus effectively returning and uninitialized pointer.
4 Likes
system
(system)
Closed
November 20, 2019, 6:19pm
6
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.