Golang GAE - intID in mustache structure

Here is an example application. The main code is in: golang-code / handler / handler.go (after the object should appear ID!)

I am trying to create a small blog system in the Golang on Google Appengine and use Mustache as a template engine.

So I have a structure:

type Blogposts struct { PostTitle string PostPreview string Content string Creator string Date time.Time } 

Data is transmitted to GAE through

  datastore.Put(c, datastore.NewIncompleteKey(c, "Blogposts", nil), &blogposts) 

So GAE automatically assigns an intID (int64). Now I tried to get the latest blog pages

 // Get the latest blogposts c := appengine.NewContext(r) q := datastore.NewQuery("Blogposts").Order("-Date").Limit(10) var blogposts []Blogposts _, err := q.GetAll(c, &blogposts) 

So far, everything is working fine, but when I try to access the intID (or stringID, regardless), I do not have access to this: - (

 <h3><a href="/blog/read/{{{intID}}}">{{{PostTitle}}}</a></h3> 

(PostTitle works, no intID, I tried thousands of things, nothing worked :-()

Any idea? It would be great!

Edit: I use a mustache.

http://mustache.github.com/

In the code I'm using:

 x["Blogposts"] = blogposts data := mustache.RenderFile("templates/about.mustache", x) sendData(w, data) // Equivalent to fmt.Fprintf 

And then you can access the data in the .mustache template with {{{Content}}} or {{{PostTitle}}}, etc.

+6
source share
4 answers

intID is an internal property of the key, not a structure, and is accessible through a getter:

 id := key.IntID() 

GetAll returns []*Key , which you are not using:

 _, err := q.GetAll(c, &blogposts) 

One way to get around this is to create a viewmodel structure that has both your message information and key information (unverified, but this is its essence):

  //... handler code ... keys, err := q.GetAll(c, &blogposts) if err != nil { http.Error(w, "Problem fetching posts.", http.StatusInternalServerError) return } models := make([]BlogPostVM, len(blogposts)) for i := 0; i < len(blogposts); i++ { models[i].Id = keys[i].IntID() models[i].Title = blogposts[i].Title models[i].Content = blogposts[i].Content } //... render with mustache ... } type BlogPostVM struct { Id int Title string Content string } 
+7
source

As the hyper syllable pointed out, the id field of the object is located on the key, and not on the structure into which it is read.

Another way: add an id field to your structure and tell the data store to ignore it , for example:

 type Blogposts struct { PostTitle string PostPreview string Content string Creator string Date time.Time Id int64 `datastore:"-"` } 

You can then fill in the ID field manually after calling GetAll () as follows

 keys, err := q.GetAll(c, &blogposts) if err != nil { // handle the error return } for i, key := range keys { blogposts[i].Id = key.IntID() } 

This has the advantage of not introducing an extra type.

+15
source

I know this question has been around for a couple of years, but the following article has helped me a lot in this regard: Golang Basics with Google Data Warehouse .

In the article, the author gives a good example of how you can run a query that receives an object by its identifier ...

 func GetCategory(c appengine.Context, id int64) (*Category, error) { var category Category category.Id = id k := category.key(c) err := datastore.Get(c, k, &category) if err != nil { return nil, err } category.Id = k.IntID() return &category, nil } 

... as well as getting a list / collection of objects with an associated ID:

 func GetCategories(c appengine.Context) ([]Category, error) { q := datastore.NewQuery("Category").Order("Name") var categories []Category keys, err := q.GetAll(c, &categories) if err != nil { return nil, err } // you'll see this a lot because instances // do not have this by default for i := 0; i < len(categories); i++ { categories[i].Id = keys[i].IntID() } return categories, nil } 

The above snippet is very close to @koz's helpful answer.

+2
source

AFAICS, the Blogposts structure does not have an intID field, but has a PostTitle field. I suggest that this may be the reason why the first does not, and later it turns out, although I never used Mustache ...

0
source

Source: https://habr.com/ru/post/912118/


All Articles