duancan1732 2018-06-04 09:28
浏览 36
已采纳

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

I am new to go and try to set up a website with some templates. On some sites I get ERR_EMPTY_RESPONSE in Chrome and this error in the 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

It seems something goes wrong on tmpl.Execute. The strange thing is, that the exact same code works for some sites and for some it doesn't.

I reduced my code on 2 site-examples. Cart works. Clients doesn't.

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

And these are the templates: Cart:

<!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>

Clients:

<!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>

I checked it all a lot. I just don't see the difference.

  • 写回答

1条回答 默认 最新

  • douzao5487 2018-06-04 09:39
    关注

    You have some syntax errors. There's an extra { in title and extra " at the end of RentedEq in clients.html.

    Fixing this should fix the panic.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败