[英]How to concatenate a variable and a string with the go templates
I need to concatenate a scoped variable and a string with go templates, something like that:我需要将一个作用域变量和一个字符串与 go 模板连接起来,如下所示:
{{ $url := .Release.Namespace + ".myurl.com" }}
How can I do that?我怎样才能做到这一点?
To simply concatenate values in templates, you may use the builtin print
function.要简单地连接模板中的值,您可以使用内置
print
function。
See this example:看这个例子:
const src = `{{ $url := print .Something ".myurl.com" }}Result: {{ $url }}`
t := template.Must(template.New("").Parse(src))
params := map[string]interface{}{
"Something": "test",
}
if err := t.Execute(os.Stdout, params); err != nil {
panic(err)
}
Output (try it on the Go Playground ): Output(在Go Playground上尝试):
Result: test.myurl.com
It of course works if your values are not string
s because print
is an alias for fmt.Sprint()
:如果您的值不是
string
s,它当然可以工作,因为print
是fmt.Sprint()
的别名:
params := map[string]interface{}{
"Something": 23,
}
This outputs (try it on the Go Playground ):此输出(在Go Playground上尝试):
Result: 23.myurl.com
I believe you're coming from the Helm world due to .Release.Namespace
and may not want the Go portion of this.我相信您由于
.Release.Namespace
而来自 Helm 世界,并且可能不想要其中的 Go 部分。
Everything inside of the {{}}
will be removed after Helm processes your YAML.在 Helm 处理您的 YAML 后,
{{}}
内的所有内容都将被删除。 To achieve a locally scoped variable, you can use two {{}}
values.要实现局部范围的变量,您可以使用两个
{{}}
值。
The first is {{ $url:= print.Release.Namespace ".myurl.com" }}
.第一个是
{{ $url:= print.Release.Namespace ".myurl.com" }}
。 It won't produce anything after Helm processes your YAML, but it will assign the local variable $url
to the value of .Release.Namespace
and the constant .myurl.com
. Helm 处理您的 YAML 后不会产生任何内容,但它会将局部变量
$url
分配给.myurl.com
.Release.Namespace
The next is {{ $url }}
, which will allow you to use the value stored in the $url
variable.接下来是
{{ $url }}
,它将允许您使用存储在$url
变量中的值。
Bringing it together {{ $url:= print.Release.Namespace ".myurl.com" }}{{ $url }}
will produce subdomain.myurl.com
should the value of .Release.Namespace
be subdomain
.如果 .Release.Namespace 的值为
subdomain
,则将其组合在一起 {{ $url:= print.Release.Namespace " .Release.Namespace
{{ $url:= print.Release.Namespace ".myurl.com" }}{{ $url }}
将产生subdomain.myurl.com
。
package main
import (
"log"
"os"
"text/template"
)
const (
// exampleTemplate is a template for a StackOverflow example.
exampleTemplate = `{{ $url := print .Release.Namespace ".myurl.com" }}{{ $url }}`
)
// templateData is the data structure to pass to the template.
type templateData struct {
Release Release
}
// Release is a fake Go data structure for this example.
type Release struct {
Namespace string
}
func main() {
// Create the template.
tmpl := template.Must(template.New("example").Parse(exampleTemplate))
// Create the data to put into the template.
data := templateData{Release: Release{Namespace: "subdomain"}}
// Execute the template.
if err := tmpl.Execute(os.Stdout, data); err != nil {
log.Fatalf("Failed to execute template.\nError: %s", err.Error())
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.