download2565 2016-09-20 16:17
浏览 60

在领事模板中,如何限制我所涉及的元素数量?

Following is my code to render the Addresses of the nodes with a service:

    {{range "service@datacenter" "passing"}}{{.Address}} {{end}}

What I want to do is limit the number of addresses that are rendered. For example, if there are 5 nodes registered as providers of "service", I would like is to print addresses of 2 of them only. What I think I should be doing is slicing the array, but I am not able to get the GO syntax right. This is something that I want but is not syntactically correct:

    {{range "service@datacenter" "passing" [0:2]}}{{.Address}} {{end}}

What is the correct way to do this?

  • 写回答

1条回答 默认 最新

  • dovs36921 2016-09-23 14:00
    关注

    Found a solution to limit the number of array elements that can be rendered using consul-template.

        {{$x := service "service@datacenter" "passing"}}{{range $index, $element := $x}}{{if lt $index 2}}{{$element.Address}},{{end}}{{end}}
    

    The range function provides an index variable along with the array element that can be used to check for the limit using the if block. Here the only two Addresses would be rendered.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)