gpt4 book ai didi

templates - Golang 带有嵌套模板的意外 EOF

转载 作者:IT王子 更新时间:2023-10-29 01:52:14 33 4
gpt4 key购买 nike

我试图在页面上显示 slice 的内容。显示带有静态文本的模板是有效的。一旦我尝试对发送到 ExTpl() 的 slice 进行范围调整,我就会收到 EOF 错误。

type Miner struct {
IP string `bson:"ip"`
Port int32 `bson:"port"`
FailCount int `bson:"failcount"`
}

type (
MinerController struct {
Session *mgo.Session
}
)

func ExTpl(w http.ResponseWriter, data []Miner) {
t, _ := template.ParseFiles("templates/header.tmpl", "templates/footer.tmpl", "templates/data.tmpl")
t.ExecuteTemplate(w, "data", data)
}

func FilterMiners(c *mgo.Collection, key, value string, limit int) ([]Miner, int, error) {
var results []Miner
query := bson.M{key: value}
if len(key) == 0 {
query = nil
}

err := c.Find(query).Limit(limit).All(&results)
if err != nil {
return results, 0,errors.New("error retrieving hosts")
}
return results, len(results), nil
}

func (mc MinerController) GetSpecificMiners(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
l, err := strconv.Atoi(vars["limit"])
if err != nil {
l = 0
}

collection := Collection("miners", "NASty", mc.Session)
fleet, _ , _ := FilterMiners(collection, vars["key"], vars["value"], l)
ExTpl(w, fleet)
}

这里是有问题的模板:

header.tmpl

{{define "header"}}
<!DOCTYPE html>
<html>
<head>
<title></title>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
</head>
<body>
{{end}}

data.tmpl

{{define "data"}}
{{template "header"}}
{{range .}}
{{.IP}}
{{template "footer"}}
{{end}}

footer.tmpl

{{define "footer"}}
</body>
</html>
{{end}}

当我运行它时,出现以下错误:

2017/10/27 12:24:56 http: panic serving [::1]:50252: template: data.tmpl:6: unexpected EOF
goroutine 49 [running]:
net/http.(*conn).serve.func1(0xc042138000)
C:/Go/src/net/http/server.go:1721 +0xd7
panic(0x7abbc0, 0xc042170360)
C:/Go/src/runtime/panic.go:489 +0x2dd
git.REDACTED.duckdns.org/REDACTED/RESTingMongo/controllers.ExTpl(0x9b7820, 0xc04211c460, 0xc042172000, 0x2, 0x2)
D:/_projects/Go/src/git.REDACTED.duckdns.org/REDACTED/RESTingMongo/controllers/handlers.go:25 +0x84
git.REDACTED.duckdns.org/REDACTED/RESTingMongo/controllers.MinerController.GetSpecificMiners(0xc04203b040, 0x9b7820, 0xc04211c460, 0xc0420f6600)
D:/_projects/Go/src/git.REDACTED.duckdns.org/REDACTED/RESTingMongo/controllers/miners.go:74 +0x2b9
git.REDACTED.duckdns.org/REDACTED/RESTingMongo/controllers.(MinerController).GetSpecificMiners-fm(0x9b7820, 0xc04211c460, 0xc0420f6600)
D:/_projects/Go/src/git.REDACTED.duckdns.org/REDACTED/RESTingMongo/routes/routes.go:33 +0x55
net/http.HandlerFunc.ServeHTTP(0xc04203f210, 0x9b7820, 0xc04211c460, 0xc0420f6600)
C:/Go/src/net/http/server.go:1942 +0x4b
git.REDACTED.duckdns.org/REDACTED/RESTingMongo/routes.Logger.func1(0x9b7820, 0xc04211c460, 0xc0420f6600)
D:/_projects/Go/src/git.REDACTED.duckdns.org/REDACTED/RESTingMongo/routes/handlers.go:12 +0x18d
net/http.HandlerFunc.ServeHTTP(0xc0420467c0, 0x9b7820, 0xc04211c460, 0xc0420f6600)
C:/Go/src/net/http/server.go:1942 +0x4b
github.com/gorilla/mux.(*Router).ServeHTTP(0xc04203c420, 0x9b7820, 0xc04211c460, 0xc0420f6600)
D:/_projects/Go/src/github.com/gorilla/mux/mux.go:133 +0x108
net/http.serverHandler.ServeHTTP(0xc04208e2c0, 0x9b7820, 0xc04211c460, 0xc042116100)
C:/Go/src/net/http/server.go:2568 +0x99
net/http.(*conn).serve(0xc042138000, 0x9b7fe0, 0xc042104280)
C:/Go/src/net/http/server.go:1825 +0x619
created by net/http.(*Server).Serve
C:/Go/src/net/http/server.go:2668 +0x2d5

我不确定发生了什么,因为我所有的模板都有正确的格式,并且 Miner 结构包含一个 IP 字段,我应该能够使用 .IP 在范围循环中访问。

最佳答案

您的data 模板定义无效。 {{define}}{{range}} 操作都需要结束 {{end}},所以它应该是:

{{define "data"}}
{{template "header"}}
{{range .}}
{{.IP}}
{{end}}
{{template "footer"}}
{{end}}

一些建议:

关于templates - Golang 带有嵌套模板的意外 EOF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46972945/

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