Answer the question
In order to leave comments, you need to log in
range problem. How to decide?
I have such a problem, specific data from the array is not transmitted on the web server, that is, when I write . in the template, then everything is fine and everything is displayed, but when I try to output, let's say the same .name, it does not output anything, and at all everything that goes beyond .name in the template does not work.
Here is the code:
The problematic piece of code from main.go -
type Ship struct {
id uint16
name string
price uint16
people uint16
kg uint16
speed uint16
desc uint16
}
var ships_full = []Ship{}
func shop_page(page http.ResponseWriter, req *http.Request) {
temp, err := template.ParseFiles("templates/html/shop.html", "templates/html/header.html", "templates/html/footer.html")
if err != nil {
fmt.Fprintf(page, err.Error())
}
db, err := sql.Open("mysql", "re-incarnation:[email protected](127.0.0.1:3306)/poplavok")
if err != nil {
panic(err)
}
defer db.Close()
res, err := db.Query("SELECT * FROM `ships`")
if err != nil {
panic(err)
}
ships_full = []Ship{}
for res.Next() {
var ship Ship
err = res.Scan(&ship.id, &ship.name, &ship.price, &ship.people, &ship.kg, &ship.speed, &ship.desc)
if err != nil {
panic(err)
}
ships_full = append(ships_full, ship)
//fmt.Println(fmt.Sprintf("Ship: %s", ship.name))
// spew.Dump(ships)
}
temp.ExecuteTemplate(page, "shop_page", ships_full)
}
{{ define "shop_page" }}
{{ template "header"}}
<h1>Shop</h1>
{{ range . }}
<h1>{{ .name }}</h1>
{{ else }}
<h1>test</h1>
{{ end }}
{{ template "footer"}}
{{ end }}
Answer the question
In order to leave comments, you need to log in
Change the structure field names - start them with a capital letter to make them public (for example, ). Name string
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question