I'm trying to transfer data to write them to the database. But I have the error "panic: runtime error: invalid memory address or nil pointer dereference" In this function, I wait for the data from the client
func handlePacket(conn net.Conn) {
rw := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
defer conn.Close()
packet := model.RegistrationMessage{}
client := JsonDecoderMessage(rw).Decode(&packet)
if client != nil {
puts("Error from Decode.Please NO :(")
}
if packet.MessageType == model.AUTH_MESSAGE {
puts("Auth")
} else if packet.MessageType == model.REGS_MESSAGE {
puts("Regs")
Registration(packet.Login, packet.Password)
puts("good")
}
}
And here I establish connection with a DB and I try to send the data in a DB
var db *sql.DB
func InitDataBase() {
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()
err = db.Ping()
if err != nil {
panic(err)
}
fmt.Println("Successfully connected!")
}
func Registration(email, password string) {
sqlStatement := `INSERT INTO account0( email,password)
VALUES ($1, $2) RETURNING id`
id := 0
err := db.QueryRow(sqlStatement, email, password).Scan(&id)
if err != nil {
panic(err)
}
}