How can I join two strings in go templates?
Asked Answered
A

4

3

I found this documentation to join two strings, but this doesn't work inside go templates.

Is there a way to join strings inside a go template?

Austral answered 18/8, 2016 at 16:16 Comment(0)
P
4

Write a function to join the strings and add it to the template func map:

func join(sep string, s ...string) string {
    return strings.Join(s, sep)
}

Add the function to template before parsing:

t, err := template.New(name).Funcs(template.FuncMap{"join": join}).Parse(text)

Use it in the template like this:

{{$x := join ", " "hello" "world"}}

playground example

I assign to variable $x to show how use the function result in a template expression. The result can be used directly as an argument to another function without assigning to a variable.

Here's a version of the function that works with slices instead of variadic arguments:

func join(sep string, s []string) string {
    return strings.Join(s, sep)
}

Use it like this where . is a slice:

{{$x := join ", " .}}  

playground example.

If your goal is to join two strings directly do the output, then then use {{a}}sep{{b}} where a and b are the strings and sep is the separator.

Use range to join a slice to the output. Here's an example that joins slice . with separator ", ".:

{{range $i, $v := .}}{{if $i}}, {{end}}{{$v}}{{end}}

playground example.

Plumbery answered 18/8, 2016 at 16:40 Comment(0)
T
6

Use a combination of delimit and slice, e.g.

{{ delimit (slice "foo" "bar" "buzz") ", " }}
<!-- returns the string "foo, bar, buzz" -->

Originally from the gohugo docs

Teirtza answered 12/1, 2017 at 13:17 Comment(1)
delimit is not part of the default template functionsInbeing
P
4

Write a function to join the strings and add it to the template func map:

func join(sep string, s ...string) string {
    return strings.Join(s, sep)
}

Add the function to template before parsing:

t, err := template.New(name).Funcs(template.FuncMap{"join": join}).Parse(text)

Use it in the template like this:

{{$x := join ", " "hello" "world"}}

playground example

I assign to variable $x to show how use the function result in a template expression. The result can be used directly as an argument to another function without assigning to a variable.

Here's a version of the function that works with slices instead of variadic arguments:

func join(sep string, s []string) string {
    return strings.Join(s, sep)
}

Use it like this where . is a slice:

{{$x := join ", " .}}  

playground example.

If your goal is to join two strings directly do the output, then then use {{a}}sep{{b}} where a and b are the strings and sep is the separator.

Use range to join a slice to the output. Here's an example that joins slice . with separator ", ".:

{{range $i, $v := .}}{{if $i}}, {{end}}{{$v}}{{end}}

playground example.

Plumbery answered 18/8, 2016 at 16:40 Comment(0)
T
2

Here is how I joined two strings in go template language (this is within a Helm template for Kubernetes). In this case I am creating a host name using go's printf function:

host: {{ printf "%v%v" "my-super" ".apps.my.org" }}

Put in a %v for each value.

The output is:

host: my-super.apps.my.org

You can put spaces between the %v values, or commas, or whatever -- check with go's string format options for more, um, options!

Teddy answered 11/3, 2022 at 16:59 Comment(1)
+1 Yet and again, if you want an SO answer that makes sense, keep scrolling down, and down, and down... :DDiphenylamine
P
1

In the template example https://golang.org/src/text/template/example_test.go they show you how to do this.

In summary:

var funcs = template.FuncMap{"join": strings.Join}
...
masterTmpl, err := template.New("master").Funcs(funcs).Parse(master)

There's some other neat tricks in the example too.

Parole answered 8/10, 2020 at 6:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.