R
R
re-incarnation2021-06-03 06:35:40
go
re-incarnation, 2021-06-03 06:35:40

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)
}


Well, here is the template code -
{{ 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

1 answer(s)
F
falconandy, 2021-06-03
@re-incarnation

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 question

Ask a Question

731 491 924 answers to any question