Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
go-调用“中的参数不足”;html/template";。必须_Html_Go_Arguments_Render_Template Engine - Fatal编程技术网

go-调用“中的参数不足”;html/template";。必须

go-调用“中的参数不足”;html/template";。必须,html,go,arguments,render,template-engine,Html,Go,Arguments,Render,Template Engine,我在Golang中编写了一个包装函数,用于从多个文件呈现模板,如下所示: func RenderTemplate(w http.ResponseWriter, data interface{}, tmpl... string) { cwd, _ := os.Getwd() for _,file:=range tmpl{ file=filepath.Join(cwd,"./view/"+file+".html") } t, err := templa

我在Golang中编写了一个包装函数,用于从多个文件呈现模板,如下所示:

func RenderTemplate(w http.ResponseWriter, data interface{}, tmpl... string) {
    cwd, _ := os.Getwd()
    for _,file:=range tmpl{
        file=filepath.Join(cwd,"./view/"+file+".html")
    }
    t, err := template.ParseFiles(tmpl...)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    templates:=template.Must(t)
    err = templates.Execute(w, data)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}
当我从
main
函数运行服务器时,控制台抛出一个错误:

调用“html/template”时参数不足。必须

如果我这样写:

func RenderTemplate(w http.ResponseWriter, data interface{}, tmpl... string) {
    cwd, _ := os.Getwd()
    for _,file:=range tmpl{
        file=filepath.Join(cwd,"./view/"+file+".html")
    }
    t, err := template.ParseFiles(tmpl...)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    templates:=template.Must(t)
    err = templates.Execute(w, data)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}
templates,err:=template.Must(t)

它还会抛出相同的错误,另外:

分配计数不匹配:2=1

我打算将此函数用于服务器中的路由处理程序:

func IndexHandler(w http.ResponseWriter, r *http.Request) {
    files:=[]string{"base","index"}
    util.RenderTemplate(w, nil, files...)
}
index.html
使用模板嵌套从
base.html
扩展而来

base.html
模板:

{{define "base"}}
<!DOCTYPE html>
<html>
<head>
    <meta charget="utf-8">
    <title>{{template "title".}}</title>
    <script type="text/javascript" src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/js/isotope.pkgd.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/css/style.css">
</head>
<body>
    {{template "index".}}
</body>
</html>
{{end}}
<!DOCTYPE html>
<html>
{{define "title"}}Homepage{{end}}
<body>
    {{define "index"}}
    <div class="wrapper">
        <div class="page-content">
            <div class="container">
                <div class="left">
                    <img src="../public/images/img_landing_page_mac.png">
                </div>
                <div class="right">
                    <h2 style="font-size: 33px; letter-spacing: 5px">Organize <br>Modern Knowledge<br> for Mankind</h2>
                    <p style="font-size: 20px;margin-top: 35px;letter-spacing: 4px">Consume, Colect and Revisit <br>Knowledge at Your Fingertips</p>
                    <a href="#" style="margin-top: 80px;display: inline-block;margin-left: -17px"><img src="../public/images/btn_get_chrome.png"></a>
                </div>
            </div>
        </div>
    </div>
    {{end}}
</body>
</html>

我错过什么了吗?我检查了
“html/template”的原型。必须
并且没有得到发生的事情

您不需要调用parsefile,必须可以调用其中一个

func RenderTemplate(w http.ResponseWriter, data interface{}, tmpl... string) {
    cwd, _ := os.Getwd()
    for _,file:=range tmpl{
        file=filepath.Join(cwd,"./view/"+file+".html")
    }
    t, err := template.ParseFiles(tmpl...)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    err = t.Execute(w, data)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}
我相信上面的func应该做你想做的事…

模板。Must()
具有以下签名:

func Must(t *Template, err error) *Template 
Must()。所以你可以说

t := template.Must(template.ParseFiles(....))
并且不关心错误检查。这只是一个方便的函数,类似于整个标准库中的所有其他
Must()
函数,例如
regexp.MustCompile()

Must()
的实现非常简单:

func Must(t *Template, err error) *Template {
    if err != nil {
            panic(err)
    }
    return t
}

请参见

您不需要使用
模板。必须在此处
;您正在对前几行进行错误检查。@TimCooper,我没有it@TimCooper当我删除
模板
并使用
t.Execute
时,我成功编译,服务器启动,当请求
index
page
openbase时,浏览器会显示:没有这样的文件或目录
I已经注册了静态文件处理程序
http.Handle(“/public/”),http.StripPrefix(/public/”),http.FileServer(http.Dir(“public”))
index
http.handlefun(“/”中的路由处理程序,route.IndexHandler)