本文介绍了Golang GAE - 用于胡须的结构体中的intID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 以下是该应用的示例。基本代码是:golang-code / handler / handler.go(在主题出现ID后) 我试图在Golang中构建一个小博客系统在谷歌Appengine和使用胡须作为模板引擎。 所以,我有一个结构: 类型Blogposts结构{ PostTitle字符串 PostPreview字符串内容字符串创建者字符串日期时间。时间} 数据通过 datastore.Put(c,datastore.NewIncompleteKey(c,Blogposts,nil)和& blogposts) 因此,GAE会自动分配一个intID(int64)。 现在我试着获得最新的博客帖子 //获取最新的博文c:= appengine。 NewContext(r)q:= datastore.NewQuery(Blogposts)。Order( - Date)。Limit(10) var blogposts [] Blogposts _, err:= q.GetAll(c,& blogposts) 直到所有的东西都能正常工作,但当我尝试访问intID(或stringID,无论)我没有访问到这个: - () < h3><< ; a href =/ blog / read / {{{intID}}}> {{{PostTitle}}}< / a>< / h3> pre> (PostTitle有效,intID不是,我试过了几千个东西,没有任何工作:-() 任何一个想法?这将是伟大的! 编辑:我使用胡子。 http://mustache.github.com/ 在代码中我使用: x [Blogposts] = blogposts data:= mustache.RenderFile(templates / about .musta che,x) sendData(w,data)//相当于fmt.Fprintf 然后可以在.mustache模板中使用{{{Content}}}或{{{PostTitle}}}来访问数据。解决方案 intID 是一个Key的内部属性,不是结构体,可以通过getter访问: id:= key.IntID() GetAll 返回 _,err c $ c> [] * Key := q.GetAll(c,& blogposts) 解决此问题的一种方法是创建一个视图模型结构,它包含你的帖子和关键信息(未经测试,但这是它的要点): // //。 ..处理程序代码... keys,err:= q.GetAll(c,& blogposts) if err!= nil { http.Error(w, 问题,http.StatusInternalServerError)返回} 模型:= make([] BlogPostVM,len(blogposts)) for i:= 0 ;我< LEN(的相关博客文章); i ++ { models [i] .Id = keys [i] .IntID() models [i] .Title = blogposts [i] .Title models [i] .Content = blogposts [i] .Content } // ...带小胡子的渲染... } 类型BlogPostVM结构体{ Id int 标题字符串内容字符串} Here is an Example of the app. The essential code is in: golang-code/handler/handler.go (After the subject should appear an ID!)Im trying to build a little blog system in Golang on Google Appengine and use Mustache as template engine.So, i have a struct: type Blogposts struct { PostTitle string PostPreview string Content string Creator string Date time.Time}The data is passed to GAE via datastore.Put(c, datastore.NewIncompleteKey(c, "Blogposts", nil), &blogposts)So, GAE assigns automatically a intID (int64).Now I tried to get the latest blogposts// Get the latest blogpostsc := appengine.NewContext(r)q := datastore.NewQuery("Blogposts").Order("-Date").Limit(10)var blogposts []Blogposts_, err := q.GetAll(c, &blogposts)Until there all things works fine, but when I try to access intID (or stringID, whatever) I dont have access to this :-(<h3><a href="/blog/read/{{{intID}}}">{{{PostTitle}}}</a></h3>(PostTitle works, intID not, i've tried thousand of things, nothing worked :-( )Anyone an idea? This would be great!Edit:I use mustache.http://mustache.github.com/In the code I use:x["Blogposts"] = blogpostsdata := mustache.RenderFile("templates/about.mustache", x)sendData(w, data) // Equivalent to fmt.FprintfAnd then the data can be accessed in the .mustache template with {{{Content}}} or {{{PostTitle}}} etc. 解决方案 intID is an internal property of a Key not the struct, and is accessible through a getter:id := key.IntID()GetAll returns []*Key, which you're not using:_, err := q.GetAll(c, &blogposts)One way to get around this is to create a viewmodel struct that has both your post and key info (untested, but this is the gist of it): //... 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} 这篇关于Golang GAE - 用于胡须的结构体中的intID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-11 03:53