映射中存储的HTML模板在首次调用时崩溃

我正在使用下面的函数来解析Go模板,并在首次调用该模板时将其保存到地图中。 </ p>

随后,将从地图上加载模板以进行优化。</ p>

  //资源... 
type资源结构{
模板map [string] template.Template
}

func(res *资源)FetchTemplate(templateName字符串)(template.Template,bool){
tmpl,确定:= res.Templates [templateName]
返回tmpl ,ok
}

func(res * Resource)ExecTemplate(w http.ResponseWriter,名称,路径字符串,模型接口{})错误{
t,ok:= res.FetchTemplate(name)
如果!ok {
t:= template.New(name)
t,err:= t.ParseFiles(res.Assets + path)

t = template.Must(t,err)

if err!= nil {

return err
}

res.Templates [name] = * t
}

if err:= t.Execute(w,model); err!= nil {
w.WriteHeader(http.StatusBadGateway)
return err
}

return nil
}
</ code> </ pre>

第一个 但是,当在模板上调用代码时,会在 t.Execute </ code>调用上惊慌失措。</ p>

它始终会随后起作用。</ p>

这是错误日志。</ p>

 <代码> /usr/local/go/src/net/http/server.go:1746 + 0xd0 
panic(0x15b3ac0,0x1b1c8d0)

/usr/local/go/src/runtime/panic.go:513 + 0x1b9
html / template。(* Template).escape(0xc000127088,0x0,0x0)
/ usr / local / go / src / html /template/template.go:95 + 0x32
html / template。(* Template).Execute(0xc000127088、0x4a90200、0xc000374680、0x15ed6c0、0xc000370a20、0x0、0x0)
/ usr / local / go / src / html / template /template.go:119 + 0x2f
git.imaxinacion.net/uoe/anssid/app/resource。(*资源).ExecTemplate(0xc0002ee120,0x4a901b0,0xc000374680,0x16577a3,0x5,0x1660b7a,0x10,0x15ed6c0,0 ...)
/Users/gbemirojiboye/go/src/git.imaxinacion.net/uoe/anssi d / app / resource / resource.go:110 + 0x1ef
</ code> </ pre>

这可能是什么原因?</ p>

何时 我正在为每个呼叫创建一个新模板,这没有发生。</ p>
</ div>

展开原文

原文

I am using the function below to parse and save a Go template to a map the first time the template is invoked.

Subsequently, the template is loaded from the map for optimization.

// Resource ...
type Resource struct {
    Templates map[string]template.Template
}

func (res *Resource) FetchTemplate(templateName string) (template.Template, bool) {
    tmpl, ok := res.Templates[templateName]
    return tmpl, ok
}

func (res *Resource) ExecTemplate(w http.ResponseWriter, name, path string, model interface{}) error {
    t, ok := res.FetchTemplate(name)
    if !ok{
        t := template.New(name)
        t, err := t.ParseFiles(res.Assets + path)

        t = template.Must(t, err)

        if err != nil {
            return err
        }

        res.Templates[name] = *t
    }

    if err := t.Execute(w, model); err != nil {
        w.WriteHeader(http.StatusBadGateway)
        return err
    }

    return nil
}

The first time the code is invoked on a template however, it panics on the t.Execute call.

It always works subsequently.

Here are the error logs.

  /usr/local/go/src/net/http/server.go:1746 +0xd0
panic(0x15b3ac0, 0x1b1c8d0)
        /usr/local/go/src/runtime/panic.go:513 +0x1b9
html/template.(*Template).escape(0xc000127088, 0x0, 0x0)
        /usr/local/go/src/html/template/template.go:95 +0x32
html/template.(*Template).Execute(0xc000127088, 0x4a90200, 0xc000374680, 0x15ed6c0, 0xc000370a20, 0x0, 0x0)
        /usr/local/go/src/html/template/template.go:119 +0x2f
git.imaxinacion.net/uoe/anssid/app/resource.(*Resource).ExecTemplate(0xc0002ee120, 0x4a901b0, 0xc000374680, 0x16577a3, 0x5, 0x1660b7a, 0x10, 0x15ed6c0, 0xc000370a20, 0x145de5e, ...)
        /Users/gbemirojiboye/go/src/git.imaxinacion.net/uoe/anssid/app/resource/resource.go:110 +0x1ef

What could be the cause of this?

When I was creating a new template for each call, this was not happening.

1个回答

The problem is that t is undefined, due to variable shadowing:

t, ok := res.FetchTemplate(name)
if !ok{
    t := template.New(name) // <---- The problem is here
    t, err := t.ParseFiles(res.Assets + path)

Within your if block, you're re-defining t with t := .... This means you have a new, locally-scoped t, and once you leave the if block, you still have the outer t which is still `nils.

Change the marked line to:

    t = template.New(name)
duancheng1955
duancheng1955 现在可以使用! 再次非常感谢
大约一年之前 回复
douxian1923
douxian1923 我一开始也很想念它。 容易犯的错误:)
大约一年之前 回复
dpfln86244
dpfln86244 哇。 我不敢相信我错过了。 它被完全隐藏在清晰的视线中。 谢谢你 将测试并还原
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐