FAQ
Hi all,

Forgive me, if GAE topics are not allowed here.

I'm trying to write a simple blog system using golang in GAE( Google App
Engine). I want to store some global data, such as, total number of
articles, which will be updated everytime a article is posted. My
assumption is :

var info datastore.Key //global data

but, how to initialize it? I wanted to initialize it in function init
which is like this:
func init() {
150
151 http.HandleFunc("/", handleMainPage)
152 http.HandleFunc("/query", handleQuery)
153 http.HandleFunc("/sign", handleSign)
154 }

but NewKey method needs a appengine.NewContext(r), and r is a http.request
which does not exist in this function.

Could anyone kindfully tell me how to make this happen? Thanks in advance.

Dean.

Search Discussions

  • Robert Snedegar at Sep 5, 2012 at 8:27 pm
    -golang-nuts@googlegroups.com,
    +google-appengine-go@googlegroups.com
    On Wed, Sep 5, 2012 at 8:49 AM, Dean Sinaean wrote:
    Hi all,

    Forgive me, if GAE topics are not allowed here.

    I'm trying to write a simple blog system using golang in GAE( Google App
    Engine). I want to store some global data, such as, total number of
    articles, which will be updated everytime a article is posted. My
    assumption is :

    var info datastore.Key //global data

    but, how to initialize it? I wanted to initialize it in function init which
    is like this:
    func init() {
    150
    151 http.HandleFunc("/", handleMainPage)
    152 http.HandleFunc("/query", handleQuery)
    153 http.HandleFunc("/sign", handleSign)
    154 }

    but NewKey method needs a appengine.NewContext(r), and r is a http.request
    which does not exist in this function.

    Could anyone kindfully tell me how to make this happen? Thanks in advance.

    Dean.
    The example code at the top of the documentation page is pretty much
    what you have to do.You have to get the context from within one of
    your handlers. Just move your declaration of the key outside the
    function, so it will stick around. Then just check if it's been
    initialized or not at the start of the handlers and if not, do the
    initialization there before moving on.

    https://developers.google.com/appengine/docs/go/datastore/reference

    var info *datastore.Key

    func handleBlah(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)

    if info == nil {
    // If you want the key to be findable by a specific id
    (for example, for users you may want to use the email address
    // so you get the same key each time you look for it,
    you use NewKey
    info := datastore.NewKey(c, "YourDatastoreTable",
    someKnowableUniqueStringForThisKey, 0, nil)

    // If you don't have a method to get the same unique
    identifier for a key everytime, (you are storing image thumbnails,
    // blog, etc, you can have the system create you a
    random one and then save that.
    info := datastore.NewIncompleteKey(c, "YourDatastoreTable", nil)
    }
    ...
    }

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 5, '12 at 3:49p
activeSep 5, '12 at 8:27p
posts2
users2
websitegolang.org

2 users in discussion

Dean Sinaean: 1 post Robert Snedegar: 1 post

People

Translate

site design / logo © 2022 Grokbase