[Solved] invalid memory address or nil pointer dereference golang database

[ad_1]

So the crash is caused by the db pointer being nil. This means code which tries to use that connection on line 40 causes a panic.

rows, errrows := db.Query(...

The db pointer is nil because, as Peter pointed out, http.ListenAndServe is blocking, which means nothing after it will run.

Try running this example locally to see the problem:

package main

import (
    "net/http"
)

func Group(res http.ResponseWriter, req *http.Request) {
    println("group handler")
}

func main() {
    http.HandleFunc("/group/", Group)
    err := http.ListenAndServe(":9001", nil)
    if err != nil {
      panic(err)
    }
    println("Running code after ListenAndServe (only happens when server shuts down)")
}

You won’t see the Running code message.

1

[ad_2]

solved invalid memory address or nil pointer dereference golang database