2

I'm new to Golang and to compiled, statically-typed programming in general. All my prior experience has been with Python.

This paradigm shift has been both frustrating (programs rarely compile) and rewarding as I'm finally getting my head around many concepts that were previously foreign to me (garbage collection, pointers, scope).

Can someone explain to me on a conceptual level why this program won't compile and the syntax to fix it? I'm simply trying to query a DB and print the results:

package main

import (
    "database/sql"
    "log"

    _ "github.com/denisenkom/go-mssqldb"
)

func main() {

    db, err := sql.Open("sqlserver", "odbc:server=myServer;user id=myName;password=myPassword;")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    q()
}

func q() {

    var (
        id   int
        name string
    )

    rows, err := db.Query("SELECT id, name FROM myTable")
    if err != nil {
        log.Fatal(err)
    }
    defer rows.Close()

    for rows.Next() {
        err := rows.Scan(&id, &name)

        if err != nil {
            log.Fatal(err)
        }
        log.Println(id, name)
    }
    err = rows.Err()
    if err != nil {
        log.Fatal(err)
    }

}

The error I'm getting is:

undefined: db in db.Query

The query works when I put the logic inside q() in the main function - I'm assuming this is because functions have "local" scope (is that the correct terminology?) and I need to define the db object as I have in the main function.

If this is the case - how do I run the q() function without repeating myself in establishing the db connection? Is this where "pointers" come in? Also, I'm still uncertain as to what the ampersands are doing here:

 err := rows.Scan(&id, &name)

Thanks

Gujarat Santana
  • 9,854
  • 17
  • 53
  • 75
mk8efz
  • 1,374
  • 4
  • 20
  • 36
  • 1
    how about passing `db` as an argument to `q()`? that would work. – Not_a_Golfer Feb 16 '17 at 22:26
  • This wouldn't work with python scoping rules either, you defined `db` in `main`, but are trying to reference it in `q`. – JimB Feb 16 '17 at 22:31
  • Related: https://stackoverflow.com/questions/40587008/how-do-i-handle-opening-closing-db-connection-in-a-go-app – JimB Feb 16 '17 at 22:33
  • Your assumption as well as the terminology is perfectly right and the same thing won't work in python too. – hashcode55 Feb 16 '17 at 23:21
  • I think you need to install plugin first to checking error of your code. there are tremendous editor and plugin out there. – Gujarat Santana Feb 17 '17 at 14:47

2 Answers2

3

db var is defined at main func scope, so your q func don't "see" it. You have to pass as pointer to q method. Something like that:

func q(db *sql.DB) {
   ...
}

Your db var in main is already a pointer to sql.DB struct, so you just have to do q(db) call in main func and it'll work.

Tarsis Azevedo
  • 1,463
  • 14
  • 21
2

As others mentioned, the problem is the variable db is declared in function main and you are trying to access that variable in q function. You have two options to solve this problem:

1) Declare the variable db as a global variable outside the main function like this:

As others mentioned, the problem is the variable db is declared in function main and you are trying to access that variable in q function. You have two options to solve this problem:

1) Declare the variable db as a global variable outside the main function like this:

package main

import (
"database/sql"
"log"
_ "github.com/denisenkom/go-mssqldb"
)

var db *sql.DB

func main() {

db, err = sql.Open("sqlserver", "odbc:server=myServer;user id=myName;password=myPassword;")
.
.
.
.

The important thing to notice is the assignment (= instead of :=) operator used to assign value to the global variable. Because if you use the short variable declaration := then it would create a local db variable.

2) You can pass the db pointer as a parameter to the function q like this:

package main

import (
    "database/sql"
    "log"
    _ "github.com/denisenkom/go-mssqldb"
)
func main() {

    db, err := sql.Open("sqlserver", "odbc:server=myServer;user id=myName;password=myPassword;")
    if err != nil {
      log.Fatal(err)
    }
    defer db.Close()

    q(db)
}

func q(db *sql.DB) {

    var (
        id   int
        name string
    )
.
.
.
.
deLta
  • 561
  • 2
  • 12