在模板中使用环境变量的 if 语句的 Go 代码。

huangapple go评论77阅读模式
英文:

Go: Using environment variables in if statement in Template

问题

以下是我用于NGINX配置的Go模板代码。在嵌套的if语句中,我试图检查环境变量IS_CUSTOMER的值是否等于"true"。

{{ if eq .instanceName "apple" }}
    {{ if eq ({{envOrKey "IS_CUSTOMER"}}) "true" }}
        listen  127.0.0.1:{{.port}};
    {{else}}
        listen  {{.bindAddress}}:{{.port}};
    {{end}}
{{else}}
    listen  {{.bindAddress}}:{{.port}};
{{end}}
listen  443 ssl;

但是当我执行模板时,出现以下错误:

tenanttemplate.tmpl:13: unexpected "{" in operand

我查阅了Go模板的在线文档和一些Stack Overflow上的答案,但没有帮助。

英文:

Below is a Go template code that I am using for NGINX configuration. In the nested if statement, I am trying to check if the value of the environment variable IS_CUSTOMER is equal to "true".

 {{ if eq .instanceName "apple" }}
	   {{ if eq ({{envOrKey "IS_CUSTOMER"}}) "true" }}
	     listen  127.0.0.1:{{.port}};
       {{else}}
         listen  {{.bindAddress}}:{{.port}};
       {{end}}
     {{else}}
         listen  {{.bindAddress}}:{{.port}};
     {{end}}
	 listen  443 ssl ;

But when I execute the template, I am getting the following error:

tenanttemplate.tmpl:13: unexpected \"{\" in operand" 

I went through online documentation of Go template and some other answers on Stack overflow, it didn't help.

答案1

得分: 3

将以下内容翻译为中文:

 {{ if eq ({{envOrKey "IS_CUSTOMER"}}) "true" }}

改为

 {{ if eq (envOrKey "IS_CUSTOMER") "true" }}
英文:

Change

 {{ if eq ({{envOrKey "IS_CUSTOMER"}}) "true" }}

to

 {{ if eq (envOrKey "IS_CUSTOMER") "true" }}

huangapple
  • 本文由 发表于 2017年9月19日 08:05:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/46289816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定