golang遍历 struct 等结构体,使用 range 关键字

for k,v := range Xxx {


}

已标记关键词 清除标记
<div class="post-text" itemprop="text"> <pre><code><!DOCTYPE html> <html> <head> <title> Test </title> </head> <body> <div> <h2>Reply</h2> <form action="/post/{{$threadID}}" method="POST"> <input type="text" name="subject" /> <input type="text" name="name" value="Anonymous" /> <input type="text" name="message" /> <input type="submit" value="submit" /> </form> </div> <div> {{range .}} {{$threadID := .ThreadID}} <h3>{{.Subject}}</h3> <h3>{{.Name}}</h3> <div>{{.DatePosted}}</div> <div><p>{{.Text}}</p></div> <br /><br /> {{end}} </div> </body> </code></pre> <p></p> <p>I have this template, there is a form at the top of the page that requires the threadID from ANY one of the Posts sent (they're all the same, a slice of all posts with a certain threadID), this obviously doesn't work, my only other idea was something along the lines of</p> <pre><code>{{range .}} {{if $threadID == nil}} $threadID := .ThreadID //build the form same as above {{end}} <h3>{{.Subject}}</h3> <h3>{{.Name}}</h3> <div>{{.DatePosted}}</div> <div><p>{{.Text}}</p></div> <br /><br /> {{end}} </code></pre> <p>Here is the Post structure and methods if any of the above is unclear.</p> <pre><code>type Post struct { threadID int subject string name string text string date_posted string } func (p *Post) ThreadID() int { return p.threadID } func (p *Post) Subject() string { return p.subject } func (p *Post) Name() string { return p.name } func (p *Post) Text() string { return p.text } func (p *Post) DatePosted() string { return p.date_posted } </code></pre> <p>And the origin of the slice of posts sent to the template</p> <pre><code>threadID := r.URL.Path[len("/reply/"):] replies, err := i.db.Query("SELECT * FROM latest_threads where thread_id="+threadID); </code></pre> </div>
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页