向数据库发送数据时出错

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)
    }
}

defer db.Close() will be executed when InitDataBase() returns, so when you use db in Registration, it causes an error.

You should call db.Close() just before your program exits, or after you are finished working with the database, whichever comes first.

Thank you all. I found the answer .You need to change db, err: = sql.Open ("postgres", psqlInfo) to db, err= sql.Open ("postgres", psqlInfo). The answer found in this link enter link description here