Go template remove the last comma in range loop
Asked Answered
S

3

19

I have code like this:

package main

import (
    "text/template"
    "os"
)

func main() {
    type Map map[string]string
    m := Map {
        "a": "b",
        "c": "d",
    }
    const temp = `{{range $key, $value := $}}key:{{$key}} value:{{$value}},{{end}}`
    t := template.Must(template.New("example").Parse(temp))
    t.Execute(os.Stdout, m)
}

it will output :

key:a value:b,key:c value:d,

but I want something like this:

key:a value:b,key:c value:d

I don't need the last comma, how to remove it. I found a solution for looping an array here: https://groups.google.com/d/msg/golang-nuts/XBScetK-guk/Bh7ZFz6R3wQJ , but I can't get index for a map.

Sweated answered 8/3, 2017 at 3:37 Comment(5)
Undoable in templates. You must do this either directly in your Go code or expose a function like TrimTrailingComma to your template.Dey
thank you, I will find other way to solve itSweated
I think the most elegant solution can be found via this question/answer: #10747554Tantivy
@jboschiero The elegant solution works for slices, not for maps.Coakley
@Dey It's possible since Go 1.11. See answer below.Advise
M
10

Here's how to write comma separated key-value pairs using a template function.

Declare a function that returns a function that increments and returns a counter:

func counter() func() int {
    i := -1
    return func() int {
        i++
        return i
    }
}

Add this function to the template:

t := template.Must(template.New("example").Funcs(template.FuncMap{"counter": counter}).Parse(temp))

Use it in the template like this:

    {{$c := counter}}{{range $key, $value := $}}{{if call $c}}, {{end}}key:{{$key}} value:{{$value}}{{end}}

This template writes the separators before the key-value pairs instead after the pairs.

The counter is created before the loop and incremented on each iteration through the loop. The separator is not written the first time through the loop.

Run it in the playground.

The logic in the template can be simplified by moving the if statement to Go code:

func separator(s string) func() string {
    i := -1
    return func() string {
        i++
        if i == 0 {
            return ""
        }
        return s
    }
}

Add the function to the template:

t := template.Must(template.New("example").Funcs(template.FuncMap{"separator": separator}).Parse(temp))

Use it like this:

{{$s := separator ", "}}{{range $key, $value := $}}{{call $s}}key:{{$key}} value:{{$value}}{{end}}

Run it on the playground.

Masaccio answered 8/3, 2017 at 5:51 Comment(1)
As @Dey says, no way to do this in template, maybe this is the best solutionSweated
A
25

Since Go 1.11 it is now possible to change values of template variables. This gives us the possibility to do this without the need of a custom function (being outside of the template).

The following template does that:

{{$first := true}}
{{range $key, $value := $}}
    {{if $first}}
        {{$first = false}}
    {{else}}
        ,
    {{end}}
    key:{{$key}} value:{{$value}}
{{end}}

Here's the altered working example from the question:

type Map map[string]string
m := Map{
    "a": "b",
    "c": "d",
    "e": "f",
}
const temp = `{{$first := true}}{{range $key, $value := $}}{{if $first}}{{$first = false}}{{else}}, {{end}}key:{{$key}} value:{{$value}}{{end}}`
t := template.Must(template.New("example").Parse(temp))
t.Execute(os.Stdout, m)

Which outputs (try it on the Go Playground):

key:a value:b, key:c value:d, key:e value:f
Advise answered 17/3, 2019 at 20:57 Comment(2)
Do you know by any chance which version of Docker I need for it to work?Leavy
great, this simplifies a lot when there are nested loops.Couvade
M
10

Here's how to write comma separated key-value pairs using a template function.

Declare a function that returns a function that increments and returns a counter:

func counter() func() int {
    i := -1
    return func() int {
        i++
        return i
    }
}

Add this function to the template:

t := template.Must(template.New("example").Funcs(template.FuncMap{"counter": counter}).Parse(temp))

Use it in the template like this:

    {{$c := counter}}{{range $key, $value := $}}{{if call $c}}, {{end}}key:{{$key}} value:{{$value}}{{end}}

This template writes the separators before the key-value pairs instead after the pairs.

The counter is created before the loop and incremented on each iteration through the loop. The separator is not written the first time through the loop.

Run it in the playground.

The logic in the template can be simplified by moving the if statement to Go code:

func separator(s string) func() string {
    i := -1
    return func() string {
        i++
        if i == 0 {
            return ""
        }
        return s
    }
}

Add the function to the template:

t := template.Must(template.New("example").Funcs(template.FuncMap{"separator": separator}).Parse(temp))

Use it like this:

{{$s := separator ", "}}{{range $key, $value := $}}{{call $s}}key:{{$key}} value:{{$value}}{{end}}

Run it on the playground.

Masaccio answered 8/3, 2017 at 5:51 Comment(1)
As @Dey says, no way to do this in template, maybe this is the best solutionSweated
C
1

I used the below template to correctly format some JSON using a go template.

It handles a collection of any size or empty.

{{- define "JoinList" -}}
   {{- $lastIndex := math.Sub (len .) 1 -}}
   {{- range $index, $property := . -}}
   {{ if isKind "string" $property.value }}
   "{{ $property.key }}": "{{ $property.value }}"{{ if ne $index $lastIndex }},{{ end }}
   {{- else -}}
   "{{ $property.key }}": {{ $property.value }}{{ if ne $index $lastIndex }},{{ end }}
   {{ end }}
   {{- end -}}
{{- end -}}
Coordinate answered 8/2, 2022 at 21:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.