Golang CSRF在struct中保存模板字段

伊恩·V:

我正在尝试使一个简单的Web服务器决定对我的路由使用骨骼对csrf 使用大猩猩 csrf。我遇到的问题是我无法将csrf.TemplateField(req)保存在要在模板中使用的结构中。

进口:

import (
    "database/sql"
    "net/http"
    "text/template"

    "github.com/go-zoo/bone"
    "github.com/gorilla/csrf"
)

结构:

type Input struct {
    Title     string
    Name      string
    csrfField template.HTML // Error here: Undefined "text/template".HTML
}

处理程序代码:

func RootHandler(rw http.ResponseWriter, req *http.Request) {
    temp, _ := template.ParseFiles("site/index.html")
    head := Input{Title: "test", csrf.TemplateTag: csrf.TemplateField(req)}
    temp.Execute(rw, head)
}

我尝试将template.HTML类型更改为字符串,然后遇到csrf.TemplateField(req)错误:

输入类型的结构文字中的未知字段'csrf.TemplateTag'

有人可以帮忙吗?我使用的类型错误吗?

松饼上衣:

HTML类型在“ html / template”中声明。导入“ html / template”而不是“ text / template”。

模板引擎将忽略未导出的字段。通过以大写字母开头的名称来导出字段名称。

import (
    "database/sql"
    "net/http"
    "html/template"

    "github.com/go-zoo/bone"
    "github.com/gorilla/csrf"
)
Struc:

type Input struct {
    Title     string
    Name      string
    CSRFField template.HTML 
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章