FAQ
Hello,

Official guide says it's okay to use a global variable to cache templates:

http://golang.org/doc/articles/wiki/

*First we create a global variable named templates, and initialize it with
ParseFiles.*

Can global variables also be used to store DB handles and repository
objects? Or do they have to be initialized for every request?

Thanks a lot

--
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/d/optout.

Search Discussions

  • Egon at Jul 8, 2014 at 4:26 pm

    On Tuesday, 8 July 2014 14:34:54 UTC+3, alse...@gmail.com wrote:
    Hello,

    Official guide says it's okay to use a global variable to cache templates:

    http://golang.org/doc/articles/wiki/

    *First we create a global variable named templates, and initialize it with
    ParseFiles.*

    Can global variables also be used to store DB handles and repository
    objects? Or do they have to be initialized for every request?

    Thanks a lot
    It depends. Global variables make testing harder and it couples things more
    together. Then again sometimes a global variable is easier to manage rather
    than passing it around everywhere. The only thing you need to be aware of
    are race conditions.

    Basically, if it's something important, don't use global variables. If it
    isn't important, you'll get things done faster with global variables and
    with some locks.

    + egon

    --
    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/d/optout.
  • Siddon Tang at Jul 9, 2014 at 3:34 am
    For go database handle, it is safe for concurrent, see
    http://golang.org/pkg/database/sql/#Open, so you can use it as global
    which must be initialized first at somewhere certainly.

    I like write a http handle holds the db pointer. e.g.

    type MyHandle struct {
    db *sql.DB
    }
    func (h *MyHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    //use h.db here
    }
    func main() {
    db := sql.Open(dsn)
    h := &MyHandle{db}
    http.Handle("/", h)
    }


    在 2014年7月8日星期二UTC+8下午7时34分54秒,alse...@gmail.com写道:
    Hello,

    Official guide says it's okay to use a global variable to cache templates:

    http://golang.org/doc/articles/wiki/

    *First we create a global variable named templates, and initialize it with
    ParseFiles.*

    Can global variables also be used to store DB handles and repository
    objects? Or do they have to be initialized for every request?

    Thanks a lot
    --
    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/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 8, '14 at 4:17p
activeJul 9, '14 at 3:34a
posts3
users3
websitegolang.org

3 users in discussion

Egon: 1 post Alsemisc: 1 post Siddon Tang: 1 post

People

Translate

site design / logo © 2022 Grokbase