dousuochu7291 2017-08-10 10:36
浏览 28
已采纳

在Go HTML模板中创建结构映射键的语法

I'm writing a simple Go program to display an HTML table of deployed service versions per environment. My program contains the following structs:

type versionKey struct {
    Environment string
    Service     string
}

type templateData struct {
    Environments []string
    Services     []string
    Versions     map[versionKey]string
}

As you can see, the Versions map uses a versionKey as a key for a string value e.g. "1.0.0".

I'm passing the templateData struct to an HTML template and ranging over its Environments and Services slices to build the HTML table. The problem is that I need to construct a versionKey for any given intersection of environment and service so I can use it to look up the version from the Versions map and output that value in the table cell.

Within the template I have $environment and $service variables available from the ranges, but I can't work out the Go template syntax to create the versionKey struct.

Here's the template code with the markup omitted:

{{$environments := .Environments}}
{{$services := .Services}}
{{$versions := .Versions}}

{{range $service := $services}}
  ...
  {{range $environment := $environments}}
    ...
    {{index $versions ...? }} // How to create versionKey struct map key here?
    ...
  {{end}}
  ...
{{end}}
  • 写回答

1条回答 默认 最新

  • douzhang2680 2017-08-10 11:13
    关注

    Using only template code you can't. You need some kind of support from the executing Go code to do that. By design philosophy, templates should not contain complex logic. You may argue whether this is complex, but the template syntax has no support for this.

    Simplest solution would be to add a Version() method to the templateData struct, which would simply return the version for a given environment and service:

    func (t *templateData) Version(environment, service string) string {
        return t.Versions[versionKey{
            Environment: environment,
            Service:     service,
        }]
    }
    

    Using this from the template:

    {{range $service := $services -}}
      {{range $environment := $environments}}
        {{$environment}} - {{$service}} version: {{$.Version $environment $service}}
      {{end}}
    {{end}}
    

    Testing it:

    t := template.Must(template.New("").Parse(templ))
    td := &templateData{
        Environments: []string{"EnvA", "EnvB"},
        Services:     []string{"ServA", "ServB"},
        Versions: map[versionKey]string{
            {"EnvA", "ServA"}: "1.0.0",
            {"EnvA", "ServB"}: "1.0.1",
            {"EnvB", "ServA"}: "1.0.2",
        },
    }
    if err := t.Execute(os.Stdout, td); err != nil {
        panic(err)
    }
    

    Output (try it on the Go Playground):

    EnvA - ServA version: 1.0.0
    
    EnvB - ServA version: 1.0.2
    
    
    EnvA - ServB version: 1.0.1
    
    EnvB - ServB version: 
    

    Alternatives

    Instead of the templateData.Version() method you could just as easily register a function which could create and return a value of type versionKey from a given environment and service. See Template.Funcs() for details. This would be more complicated though, but more flexible as this could be reused elsewhere. See an example of this here: Golang templates (and passing funcs to template). A slight variation of this would be to pass a function value as any other template data instead of registering it as a named function, which can be called.

    Another alternative would be to "transform" your Versions field into a map of maps, e.g.:

    Versions map[string]map[string]string
    

    Which first could be indexed by environment, then by service, which in the template you can achieve by 2 {{index}} actions. You would have to check if the first indexing yields any results though.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用