FAQ
I encountered a panic in the topic when I testing GoLang with Sqlite3:

In main.main()
rows, _ := utils.DBtest("SELECT name FROM users where id=?", "1")

In the utils.DBtest()

  26 func DBtest(sql string, args ...interface{}) (*sql.Rows, error) {
27 db := getConn()
28 defer db.Close()
29 stmt, _ := db.Prepare(sql)
30 return stmt.Query(args)
31 }
11 func getConn() *sql.DB {
12 db, _ := sql.Open("sqlite3", DATABASEPATH)
13 return db
14 }

But If I change line 30 to:
  func DBtest(sql string, args ...interface{}) (*sql.Rows, error) {
30 return stmt.Query("1")
The code will work.

So I think it probably because the parameter "args". But I don't know how
to make it work.
I tried log.Prinln(args), the output is "[1]" (without quote).

Here's something more I saw:
root@sure-F83Se:~/mygo/src/github.com/kickloud# go run launcher.go
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x20 pc=0x5d9cec]
goroutine 1 [running]:
database/sql.(*Rows).Next(0x0, 0xc2000a0710)
/root/go/src/pkg/database/sql/sql.go:1301 +0x1c
main.main()
/root/mygo/src/github.com/kickloud/launcher.go:24 +0x132
goroutine 2 [syscall]:
exit status 2

I'm a rookie to GoLang. Thanks for your help!




--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Kyle Lemons at Aug 27, 2013 at 8:45 pm

    On Tue, Aug 27, 2013 at 9:16 AM, wrote:

    I encountered a panic in the topic when I testing GoLang with Sqlite3:

    In main.main()
    rows, _ := utils.DBtest("SELECT name FROM users where id=?", "1")
    Is that second argument an error? You should check it.

    In the utils.DBtest()

    26 func DBtest(sql string, args ...interface{}) (*sql.Rows, error) {
    27 db := getConn()
    28 defer db.Close()
    29 stmt, _ := db.Prepare(sql)
    30 return stmt.Query(args)
    31 }
    11 func getConn() *sql.DB {
    12 db, _ := sql.Open("sqlite3", DATABASEPATH)
    13 return db
    14 }

    But If I change line 30 to:
    func DBtest(sql string, args ...interface{}) (*sql.Rows, error) {
    30 return stmt.Query("1")
    The code will work.

    So I think it probably because the parameter "args". But I don't know how
    to make it work.
    I tried log.Prinln(args), the output is "[1]" (without quote).

    Here's something more I saw:
    root@sure-F83Se:~/mygo/src/github.com/kickloud# go run launcher.go
    panic: runtime error: invalid memory address or nil pointer dereference
    [signal 0xb code=0x1 addr=0x20 pc=0x5d9cec]
    goroutine 1 [running]:
    database/sql.(*Rows).Next(0x0, 0xc2000a0710)
    /root/go/src/pkg/database/sql/sql.go:1301 +0x1c
    main.main()
    /root/mygo/src/github.com/kickloud/launcher.go:24 +0x132
    goroutine 2 [syscall]:
    exit status 2

    I'm a rookie to GoLang. Thanks for your help!




    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Surezeroleo at Sep 3, 2013 at 9:28 am

    On Wednesday, August 28, 2013 12:16:36 AM UTC+8, surez...@gmail.com wrote:
    I encountered a panic in the topic when I testing GoLang with Sqlite3:

    In main.main()
    rows, _ := utils.DBtest("SELECT name FROM users where id=?", "1")

    In the utils.DBtest()

    26 func DBtest(sql string, args ...interface{}) (*sql.Rows, error) {
    27 db := getConn()
    28 defer db.Close()
    29 stmt, _ := db.Prepare(sql)
    30 return stmt.Query(args)
    31 }
    11 func getConn() *sql.DB {
    12 db, _ := sql.Open("sqlite3", DATABASEPATH)
    13 return db
    14 }

    But If I change line 30 to:
    func DBtest(sql string, args ...interface{}) (*sql.Rows, error) {
    30 return stmt.Query("1")
    The code will work.

    So I think it probably because the parameter "args". But I don't know how
    to make it work.
    I tried log.Prinln(args), the output is "[1]" (without quote).

    Here's something more I saw:
    root@sure-F83Se:~/mygo/src/github.com/kickloud# go run launcher.go
    panic: runtime error: invalid memory address or nil pointer dereference
    [signal 0xb code=0x1 addr=0x20 pc=0x5d9cec]
    goroutine 1 [running]:
    database/sql.(*Rows).Next(0x0, 0xc2000a0710)
    /root/go/src/pkg/database/sql/sql.go:1301 +0x1c
    main.main()
    /root/mygo/src/github.com/kickloud/launcher.go:24 +0x132
    goroutine 2 [syscall]:
    exit status 2

    I'm a rookie to GoLang. Thanks for your help!
    I found the answer by myself. ...{}interface means an array, whereas "1" is
    not an array. The right syntax will be:
    id:=1
    var args []interface{}
    args = append(args, id)
    return stmt.Query(args...)

    In GoLang, you have to input something in [], a number or "..." (without
    quote).
    See more:
    https://groups.google.com/forum/#!topic/golang-china/lO1RewIY1g0


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Kyle Lemons at Sep 3, 2013 at 5:49 pm
    On Tue, Sep 3, 2013 at 2:28 AM, wrote:
    On Wednesday, August 28, 2013 12:16:36 AM UTC+8, surez...@gmail.com wrote:

    I encountered a panic in the topic when I testing GoLang with Sqlite3:

    In main.main()
    rows, _ := utils.DBtest("SELECT name FROM users where id=?", "1")

    In the utils.DBtest()

    26 func DBtest(sql string, args ...interface{}) (*sql.Rows, error) {
    27 db := getConn()
    28 defer db.Close()
    29 stmt, _ := db.Prepare(sql)
    30 return stmt.Query(args)
    31 }
    11 func getConn() *sql.DB {
    12 db, _ := sql.Open("sqlite3", DATABASEPATH)
    13 return db
    14 }

    But If I change line 30 to:
    func DBtest(sql string, args ...interface{}) (*sql.Rows, error) {
    30 return stmt.Query("1")
    The code will work.

    So I think it probably because the parameter "args". But I don't know how
    to make it work.
    I tried log.Prinln(args), the output is "[1]" (without quote).

    Here's something more I saw:
    root@sure-F83Se:~/mygo/src/git**hub.com/kickloud#<http://github.com/kickloud#>go run launcher.go
    panic: runtime error: invalid memory address or nil pointer dereference
    [signal 0xb code=0x1 addr=0x20 pc=0x5d9cec]
    goroutine 1 [running]:
    database/sql.(*Rows).Next(0x0, 0xc2000a0710)
    /root/go/src/pkg/database/sql/**sql.go:1301 +0x1c
    main.main()
    /root/mygo/src/github.com/**kickloud/launcher.go:24<http://github.com/kickloud/launcher.go:24>+0x132
    goroutine 2 [syscall]:
    exit status 2

    I'm a rookie to GoLang. Thanks for your help!
    I found the answer by myself. ...{}interface means an array, whereas "1"
    is not an array. The right syntax will be:
    []interface{} is a *slice* of empty interfaces, which can store any value.

    id:=1
    var args []interface{}
    args = append(args, id)
    return stmt.Query(args...)

    In GoLang, you have to input something in [], a number or "..." (without
    quote).
    For an array, yes. But that's not what you want, you want a slice.
    http://golang.org/ref/spec#Slices

    See more:
    https://groups.google.com/forum/#!topic/golang-china/lO1RewIY1g0



    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 27, '13 at 6:42p
activeSep 3, '13 at 5:49p
posts4
users2
websitegolang.org

2 users in discussion

Surezeroleo: 2 posts Kyle Lemons: 2 posts

People

Translate

site design / logo © 2021 Grokbase