FAQ
I'm trying to create a global "DB" type variable after opening with
sql.Open(). Conceptually something like:

var MyDB *DB // this fails - DB is not a recognized type

func init() {
     MyDB, err = sql.Open(DBparms....)
}

func accessDB()
      rows, err := MyDB.Query(query...) // use the db connection that was
opened in init()
}

I've tried a variety of things to make a global "DB" or "*DB" variable, but
I'm not getting how it's supposed to work. Does MyDB have to be passed as
an argument to all of the functions requiring it?
If so, how do you declare the functions since it doesn't recognize DB as a
valid type?
Ken

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

  • Peter Bourgon at Dec 12, 2013 at 4:14 pm

    On Thu, Dec 12, 2013 at 5:10 PM, Ken GoNoob wrote:
    I'm trying to create a global "DB" type variable after opening with
    sql.Open(). Conceptually something like:

    var MyDB *DB // this fails - DB is not a recognized type
    You probably mean *sql.DB, here.
    func init() {
    MyDB, err = sql.Open(DBparms....)
    }

    func accessDB()
    rows, err := MyDB.Query(query...) // use the db connection that was
    opened in init()
    }

    I've tried a variety of things to make a global "DB" or "*DB" variable, but
    I'm not getting how it's supposed to work. Does MyDB have to be passed as an
    argument to all of the functions requiring it?
    If so, how do you declare the functions since it doesn't recognize DB as a
    valid type?
    Ken

    --
    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.
  • Kevin Gillette at Dec 13, 2013 at 12:23 am
    Don't forget to also import "database/sql" in that file as well, otherwise
    the sql identifier will be meaningless.
    On Thursday, December 12, 2013 9:14:28 AM UTC-7, Peter Bourgon wrote:

    You probably mean *sql.DB, here.
    --
    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.
  • Ken MacDonald at Dec 13, 2013 at 7:55 pm
    Kevin, Chris, Peter,
    Thanks (duh). Taking some time to get used to Go syntax for things, but
    starting to light a spark :-)
    Ken


    On Thu, Dec 12, 2013 at 7:23 PM, Kevin Gillette
    wrote:
    Don't forget to also import "database/sql" in that file as well, otherwise
    the sql identifier will be meaningless.

    On Thursday, December 12, 2013 9:14:28 AM UTC-7, Peter Bourgon wrote:

    You probably mean *sql.DB, here.
    --
    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.
  • Chris dollin at Dec 12, 2013 at 4:16 pm
    Missing package name for type?
    On 12 Dec 2013 16:10, "Ken GoNoob" wrote:

    I'm trying to create a global "DB" type variable after opening with
    sql.Open(). Conceptually something like:

    var MyDB *DB // this fails - DB is not a recognized type

    func init() {
    MyDB, err = sql.Open(DBparms....)
    }

    func accessDB()
    rows, err := MyDB.Query(query...) // use the db connection that was
    opened in init()
    }

    I've tried a variety of things to make a global "DB" or "*DB" variable,
    but I'm not getting how it's supposed to work. Does MyDB have to be passed
    as an argument to all of the functions requiring it?
    If so, how do you declare the functions since it doesn't recognize DB as a
    valid type?
    Ken

    --
    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
postedDec 12, '13 at 4:11p
activeDec 13, '13 at 7:55p
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase