I’ve the following code which works to generate the names One after another:
{{- range .File.apps}}
{{ .Name}}
{{- end}}
This prints
app1
app2
app3
The problem is that I need to get it In the same line and not one after other
app1 app2 app3
When I try like following it remove the firsts entry and put only the last value. i.e. i'll get only app3
{{- range .File.apps}} {{ .Name}} {{- end}}
How can I do that?
The -
character prevents new lines so adding a -
before the closing }}
should do the trick.
https://play.golang.org/p/a-P-yPJtm9W
package main
import "fmt"
import "os"
import "text/template"
const temp = `
{{- range .Apps -}}
{{- .Name -}}
{{- end -}}
`
type file struct {
Apps []app
}
type app struct {
Name string
}
func main() {
data := file{
Apps: []app{
app{"foo"},
app{"bar"},
},
}
t := template.Must(template.New("foo").Parse(temp))
err := t.Execute(os.Stdout, data)
if err != nil {
fmt.Println(err.Error())
}
}