登录
首页 >  Golang >  Go问答

如何在 Golang 中实现模板嵌套?

来源:stackoverflow

时间:2024-03-09 16:00:26 303浏览 收藏

小伙伴们对Golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《如何在 Golang 中实现模板嵌套?》,就很适合你,本篇文章讲解的知识点主要包括。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!

问题内容

我正在尝试将 json 响应模板化到前端,我当前的结构是这样的:

type response struct {
    whoisrecord struct {
        createddate time.time `json:"createddate"`
        updateddate time.time `json:"updateddate"`
        expiresdate time.time `json:"expiresdate"`
        registrant  struct {
            name         string `json:"name"`
            organization string `json:"organization"`
            street1      string `json:"street1"`
            city         string `json:"city"`
            state        string `json:"state"`
            country      string `json:"country"`
            countrycode  string `json:"countrycode"`
            email        string `json:"email"`
            telephone    string `json:"telephone"`
            fax          string `json:"fax"`
        } `json:"registrant"`
        administrativecontact struct {
            name         string `json:"name"`
            organization string `json:"organization"`
            street1      string `json:"street1"`
            city         string `json:"city"`
            state        string `json:"state"`
            country      string `json:"country"`
            countrycode  string `json:"countrycode"`
            email        string `json:"email"`
            telephone    string `json:"telephone"`
            fax          string `json:"fax"`
        } `json:"administrativecontact"`
        technicalcontact struct {
            name         string `json:"name"`
            organization string `json:"organization"`
            street1      string `json:"street1"`
            city         string `json:"city"`
            state        string `json:"state"`
            country      string `json:"country"`
            countrycode  string `json:"countrycode"`
            email        string `json:"email"`
            telephone    string `json:"telephone"`
            fax          string `json:"fax"`
        } `json:"technicalcontact"`
        domainname  string `json:"domainname"`
        nameservers struct {
            hostnames []string      `json:"hostnames"`
        } `json:"nameservers"`
        registrarname         string `json:"registrarname"`
        ips                []string `json:"ips"`
    } `json:"whoisrecord"`
}

然后我解组这个 json 响应,并将其传递到前端(我使用的是 gin)

响应被重新声明为res

c.html(200,"homework.html", gin.h{
        "whois":    res,
    })

但这就是我遇到问题的地方,代码可以工作,但我不确定如何对其进行模板化,因为它是嵌套的。

例如,我想将注册者、管理和技术联系详细信息(返回的所有字段)显示到单独的表中。同时显示创建、更新和过期日期。然后通过显示注册商、ip 和名称服务器(在本例中为 nameservers 下的 hostnames 字段)来完成

我该如何在我的 homework.html 文件中提供这个服务?我已经尝试了一切。通常我会做类似的事情:

显示 ip:

{{ range .ips }}
                                            

ip

{{ . }}
{{end}}

显示寄存器数据:

name

{{ .name }}

email

{{ .email }}
//etc

显示注册器:

Registrar

{{ .RegistrarName }}

但是这些都不起作用(我如何在一个字段中显示注册人姓名,然后在另一个字段中显示技术名称?我显然把模板弄乱了,而且我对它的理解有点偏差)。我已经阅读了我能读到的所有内容,我尝试划分结构并用作单独的结构等。没有任何效果。有人能给我指出正确的方向并举例吗?

谢谢!


解决方案


模板字段是相对于当前上下文 {{.}} 进行评估的。以下使用包含“whois”作为键、res 作为值的映射来评估模板:

in.h{ "whois": res})

{{.whois}} 的值是您的结构,您可以从那里访问 struc 字段。所以你可以这样做:

{{.whois.Registrant.Name}}

今天关于《如何在 Golang 中实现模板嵌套?》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>