Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't get data from field in gohtml file

I’m trying to build a very basic page that takes user input from a form and displays it within an html template

Here’s the code that should take the input data from a form and make it available as a variable in the template. The input is printed to console for sanity check, it does show correctly in there.

func processor(w http.ResponseWriter, r *http.Request) {
    if r.Method != "POST" {
        http.Redirect(w, r, "/", http.StatusSeeOther)
        return
    }

    userName := r.FormValue("user")
    userPronouns := r.FormValue("pronouns")

    d := struct {
        hostName     string
        hostPronouns string
    }{
        hostName:     userName,
        hostPronouns: userPronouns,
    }

    tpl.ExecuteTemplate(w, "processor.gohtml", d)

    fmt.Println(d.hostName)
    fmt.Println(d.hostPronouns)
}

Here is the html code that should display part of the information from the form. Eventually it’s going to be more complicated but for now I just need it to work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<body>
    <h1>BALLS</h1>
    <h2> {{.hostName}} </h2>
</body>

I can’t figure out why the data doesn’t show up. If I change the {{.hostName}} for {{.}} all the data from the form is there, so it is going through.

>Solution :

struct fields have to be exported to be available in templates.
Change your struct to

d := struct {
  HostName     string
  HostPronouns string
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading