title: Accessing struct variable outside of {{range .}} scope in golang html template
body: <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt; Test &lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div&gt;
          &lt;h2&gt;Reply&lt;/h2&gt;
          &lt;form action="/post/{{$threadID}}" method="POST"&gt;
          &lt;input type="text" name="subject" /&gt;
          &lt;input type="text" name="name" value="Anonymous" /&gt;
          &lt;input type="text" name="message" /&gt;
          &lt;input type="submit" value="submit" /&gt;
          &lt;/form&gt;
      &lt;/div&gt;
      &lt;div&gt;
          {{range .}}
          {{$threadID := .ThreadID}}
          &lt;h3&gt;{{.Subject}}&lt;/h3&gt;
          &lt;h3&gt;{{.Name}}&lt;/h3&gt;
          &lt;div&gt;{{.DatePosted}}&lt;/div&gt;
          &lt;div&gt;&lt;p&gt;{{.Text}}&lt;/p&gt;&lt;/div&gt;
          &lt;br /&gt;&lt;br /&gt;
          {{end}}
      &lt;/div&gt;
  &lt;/body&gt;
  </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}}
      &lt;h3&gt;{{.Subject}}&lt;/h3&gt;
      &lt;h3&gt;{{.Name}}&lt;/h3&gt;
      &lt;div&gt;{{.DatePosted}}&lt;/div&gt;
      &lt;div&gt;&lt;p&gt;{{.Text}}&lt;/p&gt;&lt;/div&gt;
      &lt;br /&gt;&lt;br /&gt;
  {{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>