gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 03:36:53 25 4
gpt4 key购买 nike

我刚开始尝试使用一些模板建立一个网站。在某些网站上,我在 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 个网站示例的代码。 购物车 有效。 客户端 没有。

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>

我检查了很多。我只是看不出有什么区别。

最佳答案

您有一些语法错误。 clients.html 中 RentedEq 的标题中有一个额外的 { 和额外的 "

解决这个问题应该可以解决 panic 。

关于html - 为什么我的 go-code 不适用于每个模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50677366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com