I have this struct :
const (
paragraph_hypothesis = 1<<iota
paragraph_attachment = 1<<iota
paragraph_menu = 1<<iota
)
type Paragraph struct {
Type int // paragraph_hypothesis or paragraph_attachment or paragraph_menu
}
I want to display my paragraphs in a Type
dependent way.
The only solution I found was based on dedicated functions like isAttachment
testing the Type
in Go and nested {{if}}
:
{{range .Paragraphs}}
{{if .IsAttachment}}
-- attachement presentation code --
{{else}}{{if .IsMenu}}
-- menu --
{{else}}
-- default code --
{{end}}{{end}}
{{end}}
In fact I have more types, which makes it even weirder, cluttering both the Go code with IsSomething
functions and the template with those {{end}}
.
What's the clean solution ? Is there some switch
or if/elseif/else
solution in go templates ? Or a completely different way to handle these cases ?
Paragraph
type is not too clear. You can generally create custom template behaviour by adding functions to the FuncMap (golang.org/pkg/text/template/#Template.Funcs). Sorry, I'm not sure exactly what you're trying to do. – Wycoff