1
I have a struct that has a method save
receiving as parameter a pointer for access to the seat;
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
}
// ...
}
but when I pass the pointer as parameter an error panic
is displayed;
http: Panic serving [::1]:51111: Runtime error: invalid memory address or nil Pointer dereference
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())
}
}
The passage of the parameter occurs thus:
err := c.Save(db)
// c é a struct que possui o método Save
Even defining in the method that the parameter is a pointer this error of nil Pointer happens, because?
linter should not report that there are 2 variables with the same name?
– RFL