0

0

Go 模板 if 条件

王林

王林

发布时间:2024-02-06 11:24:13

|

584人浏览过

|

来源于stackoverflow

转载

go 模板 if 条件

问题内容

如何将 andeq/ne 函数组合在一起?

我写了这个片段

{{ define "opsgenie.default.tmpl" }}
  {{.commonlabels.alertname }}
  {{- range $i, $alert := .alerts }}
  {{ .annotations.description }}
  {{- end -}}
  {{- "\n" -}}
  {{- "\n" -}}
  {{- if and eq .commonlabels.infoalert "true" eq .commonlabels.topic "database" -}}
  grafana: https://{{ .commonlabels.url }}
  {{- "\n" -}}{{- end -}}
  {{- if and ne .commonlabels.infoalert "true" eq .commonlabels.topic "database" -}}
  database:
    • https://{{ .commonlabels.url }}/
    • https://{{ .commonlabels.url }}/
  {{- "\n" -}}{{- end -}}
  {{- end -}}
  {{- end -}}
{{- end -}}

目标是:

  • 如果我的警报包含两个标签 infoalert: truetopic:database 则仅显示 grafana 链接
  • 如果我的警报仅包含标签 topic: database 但不包含 infoalert: true 则仅显示 databsse 链接

它看起来像条件​​ {{- if and eq .commonlabels.infoalert "true" eq .commonlabels.topic "database" -}} 的语法不正确,因为我在警报时在alertmanager.log中收到此错误被解雇:

培训招生教育类网站模板(响应式)1.4.2
培训招生教育类网站模板(响应式)1.4.2

培训招生教育类网站模板(响应式)安装即用,自带人人站CMS内核,支持响应式,前端banner轮播图文本均已进行可视化配置,伪静态页面生成,支持内容模型、多语言、自定义表单、筛选、多条件搜索等功能,支持多种URL模式及模型。模板特点:1、安装即用,自带人人站CMS内核及企业站展示功能(产品,新闻,案例展示等),并可根据需要增加表单 搜索等功能(自带模板) 2、支持响应式 3、前端banner轮播图文

下载
notify retry canceled due to unrecoverable error after 1 attempts: templating error: template: email.tmpl:24:17: executing \"opsgenie.default.tmpl\" at : wrong number of args for eq: want at least 1 got 0

正确答案


只需使用括号对表达式进行分组:

{{- if and (eq .commonlabels.infoalert "true") (eq .commonlabels.topic "database") -}}

{{- if and (ne .commonlabels.infoalert "true") (eq .commonlabels.topic "database") -}}

查看这个可测试的示例:

func main() {
    t := template.must(template.new("").parse(src))

    m := map[string]any{
        "infoalert": "true",
        "topic":     "database",
    }
    if err := t.execute(os.stdout, m); err != nil {
        panic(err)
    }

    fmt.println("second round")
    m["infoalert"] = "false"
    if err := t.execute(os.stdout, m); err != nil {
        panic(err)
    }
}

const src = `
{{- if and (eq .infoalert "true") (eq .topic "database") -}}
    infoalert is true and topic is database
{{- end -}}
{{- if and (ne .infoalert "true") (eq .topic "database") -}}
    infoalert is not true and topic is database
{{ end }}
`

这将输出(在 go playground 上尝试):

infoalert is true and topic is database
Second round
infoalert is NOT true and topic is database

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
if什么意思
if什么意思

if的意思是“如果”的条件。它是一个用于引导条件语句的关键词,用于根据特定条件的真假情况来执行不同的代码块。本专题提供if什么意思的相关文章,供大家免费阅读。

757

2023.08.22

discuz database error怎么解决
discuz database error怎么解决

discuz database error的解决办法有:1、检查数据库配置;2、确保数据库服务器正在运行;3、检查数据库表状态;4、备份数据;5、清理缓存;6、重新安装Discuz;7、检查服务器资源;8、联系Discuz官方支持。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

212

2023.11.20

Grafana重置admin密码
Grafana重置admin密码

本专题整合了grafana admin密码相关教程,阅读专题下面的文章了解更多详细内容。

45

2025.09.02

Grafana admin密码
Grafana admin密码

本专题整合了Grafana密码相关教程,阅读专题下面的文章了解更多详细内容。

255

2025.12.09

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

html编辑相关教程合集
html编辑相关教程合集

本专题整合了html编辑相关教程合集,阅读专题下面的文章了解更多详细内容。

56

2026.01.21

三角洲入口地址合集
三角洲入口地址合集

本专题整合了三角洲入口地址合集,阅读专题下面的文章了解更多详细内容。

30

2026.01.21

AO3中文版入口地址大全
AO3中文版入口地址大全

本专题整合了AO3中文版入口地址大全,阅读专题下面的的文章了解更多详细内容。

393

2026.01.21

妖精漫画入口地址合集
妖精漫画入口地址合集

本专题整合了妖精漫画入口地址合集,阅读专题下面的文章了解更多详细内容。

116

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号