I have a struct that has a save
method that receives a pointer for access to the database as a parameter;
func (c Call) Save(db *sql.DB) error {
stmt, err := db.Prepare('
INSERT INTO calls values($1, $2, $3, $4)
')
if err != nil {
return err
}
// ...
}
However when I pass the pointer as a parameter a panic
error is displayed;
link
The connection is defined as follows:
import (
"database/sql"
_ "github.com/lib/pq"
)
var db *sql.DB
func init() {
db, _ := sql.Open("postgres", dsn)
if err := db.Ping(); err != nil {
log.Fatal(err.Error())
}
}
Parameter passing occurs as follows:
err := c.Save(db)
// c é a struct que possui o método Save
Even though defining in the method that the parameter is a pointer this nil pointer error happens, because?