FAQ
I'm looping through a slice of objects, attempting to handle each object
with the following code:

func positionHandler(w http.ResponseWriter, r *http.Request, jobApp JobApp)
{
tmpl, _ := template.New("Position Details").Parse(positionTmpl)
tmpl.Execute(w, jobApp)
}

for _, jobApp := range jobApps {
http.HandleFunc(
fmt.Sprintf("/%d/", jobApp.JobNumber),
func(w http.ResponseWriter, r *http.Request) {
positionHandler(w, r, jobApp)
})
}

...what's happening is that the URLs are being handled, but all of them
bring up their respective template filled with the attributes of the last
object looped through. What is causing this?

--

Search Discussions

  • Minux at Nov 7, 2012 at 9:12 pm

    On Thu, Nov 8, 2012 at 2:06 AM, Waters wrote:

    I'm looping through a slice of objects, attempting to handle each object
    with the following code:

    func positionHandler(w http.ResponseWriter, r *http.Request, jobApp
    JobApp) {
    tmpl, _ := template.New("Position Details").Parse(positionTmpl)
    tmpl.Execute(w, jobApp)
    }

    for _, jobApp := range jobApps {
    http.HandleFunc(
    fmt.Sprintf("/%d/", jobApp.JobNumber),
    func(w http.ResponseWriter, r *http.Request) {
    positionHandler(w, r, jobApp)
    })
    }

    ...what's happening is that the URLs are being handled, but all of them
    bring up their respective template filled with the attributes of the last
    object looped through. What is causing this?
    Read this: http://golang.org/doc/go_faq.html#closures_and_goroutines
    although it's about goroutines, the essence is the same as your example.

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 7, '12 at 9:07p
activeNov 7, '12 at 9:12p
posts2
users2
websitegolang.org

2 users in discussion

Minux: 1 post Waters: 1 post

People

Translate

site design / logo © 2021 Grokbase