1

When i am trying to call below handler getting runtime error

Handler

func List(w http.ResponseWriter, r *http.Request) {
    item := data.GetList()
    json.NewEncoder(w).Encode(item)
}

calling function

func GetList() []m.Employees {
    rows, err := db.Query(`SELECT * FROM "employees";`)
    if err != nil {
        fmt.Println(err)
    }
    var item m.Employees
    defer rows.Close()
    var slc []m.Employees
    defer rows.Close()
    for rows.Next() {
        err = rows.Scan(&item.ID, &item.Name, &item.Rank, &item.Address, &item.Salary)
        if err != nil {
            fmt.Println("Error", err)
        }
        slc = append(slc, item)
    }
    return slc
}
James Z
  • 12,209
  • 10
  • 24
  • 44
dastagir shah
  • 45
  • 1
  • 6
  • Please indicate where the panic occurs. Likely your `db` package level variable is `nil` (possibly due to you initializing it in a function and using short variable declaration, shadowing the outer `db` variable, leaving it `nil`). If this is not the case, also show how you initialize `db`. – icza Jan 05 '22 at 15:35
  • i have defined db as var db *sql.DB in Dal layer where the function is running – dastagir shah Jan 05 '22 at 16:56
  • The question is how you initialize it. And please include in the question where the panic occurs if you want any help. – icza Jan 05 '22 at 16:56
  • item := data.GetList() rows, err := db.Query(`SELECT * FROM "employees";`) Panic points these lines – dastagir shah Jan 05 '22 at 16:59
  • And now please show us how you initialize the `db` variable. I asked this in all my comments above. – icza Jan 05 '22 at 17:07
  • @icza - i have defined db in dal layer as var db *sql.DB and In database declaration and establishing connection I am using the below command db, err := sql.Open("postgres", s) where db declaration working fine but for PostgreSQL statements i have to define var db *sql.DB sepearatley, Can you help how can i avoid this? – dastagir shah Jan 05 '22 at 17:21
  • Then this is a duplicate of [How to use global var across files in a package?](https://stackoverflow.com/questions/34195360/how-to-use-global-var-across-files-in-a-package/34195389#34195389) and [global error variable remains nil after initialization](https://stackoverflow.com/questions/42780555/global-error-variable-remains-nil-after-initialization/42781860#42781860). – icza Jan 05 '22 at 17:24
  • let me check @icza – dastagir shah Jan 05 '22 at 17:25
  • @icza - I have 2 different packages, 1 for establishing a connection where db is required to connect database, and the other package is a DAL layer where my database operations are working so in need db there as well, how can I avoid the above error? – dastagir shah Jan 05 '22 at 17:36
  • Your `db` variable remains `nil` because you use short variable declaration which does not assign to the `db` variable but creates a new local variable. Use assignment instead of the short variable declaration (use `=` instead of `:=`). – icza Jan 05 '22 at 17:39
  • @icza - I have defined a single variable now as var DB *sql.DB and importing same in another package but it still shows same error – dastagir shah Jan 05 '22 at 17:54
  • 1
    I have explained why the error happens and how to fix it. If you want further help, edit your question and include your new, changed code. It's very inconvenient to do this in comments, especially since you probably didn't follow what I suggested. – icza Jan 05 '22 at 18:01
  • i will go through it once again, Thank you so much @icza – dastagir shah Jan 05 '22 at 18:04
  • If you get an error, you print it, but you still attempt to process the row. Usually any time an error comes back, other return values are not valid. – erik258 Jan 05 '22 at 19:52

0 Answers0