为什么我的go-code不适用于每个模板?

时间:2018-06-04 09:28:16

标签: html templates go

我是新手,尝试使用一些模板设置网站。在某些网站上,我在Chrome中获得了ERR_EMPTY_RESPONSE,并且在cmd中出现了此错误:

2018/06/04 10:55:22 http: panic serving [::1]:9954: runtime error: invalid memory address or nil pointer dereference
goroutine 178 [running]:
net/http.(*conn).serve.func1(0xc042050000)
        C:/Go/src/net/http/server.go:1726 +0xd7
panic(0x71dca0, 0x955b10)
        C:/Go/src/runtime/panic.go:502 +0x237
html/template.(*Template).escape(0x0, 0x0, 0x0)
        C:/Go/src/html/template/template.go:95 +0x35
html/template.(*Template).Execute(0x0, 0x7c17c0, 0xc04221a000, 0x732d60, 0xc042222200, 0xc042450590, 0x9)
        C:/Go/src/html/template/template.go:119 +0x36
main.clientsHandler(0x7c42c0, 0xc04221a000, 0xc042254000)
        C:/Users/KevinZielke/Documents/Studium/MT/WEB/Praktikum/server/server.go:114 +0x1fc
net/http.HandlerFunc.ServeHTTP(0x798580, 0x7c42c0, 0xc04221a000, 0xc042254000)
        C:/Go/src/net/http/server.go:1947 +0x4b
net/http.(*ServeMux).ServeHTTP(0x9648c0, 0x7c42c0, 0xc04221a000, 0xc042254000)
        C:/Go/src/net/http/server.go:2337 +0x137
net/http.serverHandler.ServeHTTP(0xc04204ca90, 0x7c42c0, 0xc04221a000, 0xc042254000)
        C:/Go/src/net/http/server.go:2694 +0xc3
net/http.(*conn).serve(0xc042050000, 0x7c4480, 0xc042130080)
        C:/Go/src/net/http/server.go:1830 +0x658
created by net/http.(*Server).Serve
        C:/Go/src/net/http/server.go:2795 +0x282

tmpl.Execute似乎出现了问题。 奇怪的是,完全相同的代码适用于某些网站,有些则不适用。

我在2个网站示例中减少了我的代码。 Cart有效。 Clients没有。

package main

import (
  "html/template"
  "log"
  "net/http"
)

type User struct{
  Name string
  Role string
  Pic string
  RentedEq string
  Expire string
  Status string
  Id int
}
type Eq struct{
  Name string
  Desc string
  Rentdate string
  Turnbackdate string
  Pic string
  Count int
  User string
}
}
type Clients struct{
  User User
  Users []User
}
type Cart struct{
  User User
  Carts []Eq
}

func main() {
  fs := http.FileServer(http.Dir("static"))
  http.Handle("/static/", http.StripPrefix("/static/", fs))

  http.HandleFunc("/cart/", cartHandler)
  http.HandleFunc("/clients/", clientsHandler)

  log.Println("Listening...")
  http.ListenAndServe(":3000", nil)
}
func clientsHandler(w http.ResponseWriter, r *http.Request) {
  data := Clients{
    User: User{Name : "Dr. Acula", Role : "Admin", Pic : "../static/pics/dummy.png",},
    Users: []User{
      {Name: "Peter Pan", RentedEq: "Kamera 1", Expire: "05.06.2019"},
      {Name: "Petra Pan", RentedEq: "Kamera 3", Expire: "08.10.2019"},
    },
  }
  tmpl, _ := template.ParseFiles("./template/clients.html")
  err := tmpl.Execute(w, data)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
}
func cartHandler(w http.ResponseWriter, r *http.Request) {
  data := Cart{
    User: User{Name : "Peter Pan", Role : "Benutzer", Pic : "../static/pics/dummy.png",},
    Carts: []Eq{
      {Name: "Kamera 1", Desc: "schießt Fotos", Pic: "../static/pics/dummy.png", Count:1, Turnbackdate:"12.08.2018",},
      {Name: "Kamera 2", Desc: "schießt auch Fotos", Pic: "../static/pics/dummy.png", Count:3, Turnbackdate:"22.10.2018",},
    },
  }
  tmpl, _ := template.ParseFiles("./template/cart.html")
  err := tmpl.Execute(w, data)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
}

这些是模板: 购物车:

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="../static/css/bootstrap-4.1.0-dist/css/bootstrap.min.css" />
  <link rel="stylesheet" href="../static/css/custom.css" />
  <title>Warenkorb - borgdir.media</title>
</head>

<body class="container-fluid col-10">

  <header class="container-fluid">
    <div class="row">
      <div class="col-4 k">
        <a class="" href="/equipment"><h1>borgdir.media</h1></a>
      </div>
      <div class="col-1 k text-center">
        <a class="" href="/equipment">Equpiment</a>
      </div>
      <div class="col-1 k text-center">
        <a href="/my-equipment">Meine Geräte</a>
      </div>
      <div class="col-1 k text-center">
        <a href="/">Logout</a>
      </div>
      <div class="col-1 k text-center">
        <a href="/cart"><img class="img-fluid" src="../static/icons/cart.png" alt="Warenkorb"></img></a>
      </div>

      <div class="col-3 k text-right">
        <a href="/profil">{{.User.Name}}</br>{{.User.Role}}</a>
      </div>
      <div class="col-1 k">
        <img class="img-fluid" src={{.User.Pic}}></img>
      </div>
    </div>
  </header>

  <div>
    <h2>Warenkorb</h2>

    <div class="container-fluid">
      <table class="table">
        <tr class="d-flex">
          <th class="col-3">Equipment</th>
          <th class="col-4">Beschreibung</th>
          <th class="col-2 text-center">Anzahl</th>
          <th class="col-2 text-center">Rückgabe bis</th>
          <th class="col-1">Löschen</th>
        </tr>
        {{range .Carts}}
        <tr class="d-flex">
          <th class="col-3"><img class="img-fluid" src={{.Pic}}></img>{{.Name}}</th>
          <th class="col-4">{{.Desc}}</th>
          <th class="col-2">{{.Count}}</th>
          <th class="col-2">{{.Turnbackdate}}</th>
          <th class="col-1"><button><img class="img-fluid" src="../icons/delete.png"></img></button></th>
        </tr>
        {{end}}
      </table>
      <br>
      <input type="submit" value="Jetzt ausleihen">
    </div>

  </div>
</body>
</body>

</html>

客户端:

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="../static/css/bootstrap-4.1.0-dist/css/bootstrap.min.css" />
  <link rel="stylesheet" href="../static/css/custom.css" />
  <title>}Kunden verwalten - borgdir.media</title>
</head>

<body class="container-fluid col-10">

  <header class="container-fluid">
    <div class="row">
      <div class="col-4 k">
        <a class="" href="/admin-equipment"><h1>borgdir.media</h1></a>
      </div>
      <div class="col-1 k text-center">
        <a class="" href="/admin-equipment">Equpiment</a>
      </div>
      <div class="col-1 k text-center">
        <a href="/clients">Kunden</a>
      </div>
      <div class="col-1 k text-center">
        <a href="/">Logout</a>
      </div>

      <div class="col-3 k text-right">
        <a href="/profil">{{.User.Name}}</br>{{.User.Role}}</a>
      </div>
      <div class="col-1 k">
        <img class="img-fluid" src={{.User.Pic}}></img>
      </div>
    </div>
  </header>

  <div>
    <h2>Kunden</h2>

    <div class="row col-12">
      <div>
        Kategorie</br>
        <input type="text" name="suche" value="Suche">
      </div>
      <select>
         <option value="Alle">Alle</option>
         <option value="keine">keine</option>
        </select>
      <input type="submit" value="Suchen">
    </div>

    <div class="container-fluid">
      <table class="table">
        <tr class="d-flex">
          <th class="col-4">Equipment</th>
          <th class="col-5">Ausgeliehenes Equipment</th>
          <th class="col-3">Status</th>
        </tr>
        {{range .Users}}
        <tr class="d-flex">
          <th class="col-4">{{.Name}}</th>
          <th class="col-5">{{.RentedEq"}}</th>
          <th class="col-2">Konto aktiv bis:</br>{{.Expire}}</th>
          <th class="col-1"><button><img class="img-fluid" src="../static/icons/edit.png"></img></button></th>
        </tr>
        {{end}}
      </table>
    </div>

  </div>
</body>
</body>

</html>

我检查了很多。我只是没有看到差异。

1 个答案:

答案 0 :(得分:0)

您有一些语法错误。标题中有额外的{,在clients.html的"末尾有额外的RentedEq

解决这个问题应该可以解决恐慌。