I am trying to do some creative formatting using text template and wanted 
to see if there might be a better approach than what I implemented.

I have an array of structs, where each element has a Project and Task. The 
array is sorted by Project.
type DisplayTask struct {
 Project string
 Task string

I want to output all the tasks within a project without repeating the 
project name each time and I want the tasks to be indented, for example:


After much experimentation, I am using the following template and trying to 
determine if there is a way to avoid the printf and just have the indent as 
part of the template, but I can't figure out a way to do this and suppress 
white space (newline) from the if statement for the same project.
{{- $lastProject := "" -}}
{{ range . }}
{{- if (ne $lastProject .Project) -}}
{{ .Project }} {{ $lastProject = .Project }}
{{ end -}}
  {{ printf "  %s" .Task }}
{{ end -}}

Go Playground of the working code is at 

The challenge I had was suppressing white space after the end, but still 
allowing an indent via white space, so resorted to the printf.

Is there a better way?

You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 

Reply via email to