我有这个结构:

const (

paragraph_hypothesis = 1<

paragraph_attachment = 1<

paragraph_menu = 1<

)

type Paragraph struct {

Type int // paragraph_hypothesis or paragraph_attachment or paragraph_menu

}

我想以类型依赖的方式显示我的段落.

我发现唯一的解决方案是基于专门的功能,如isAttachment测试类型Go和嵌套{{if}}:

{{range .Paragraphs}}

{{if .IsAttachment}}

-- attachement presentation code --

{{else}}{{if .IsMenu}}

-- menu --

{{else}}

-- default code --

{{end}}{{end}}

{{end}}

实际上,我有更多的类型,这使得它甚至更凶猛,使用IsSomething函数和Go {{end}}的模板混淆了Go代码.

什么是干净的解决方案? go模板中是否有某些开关或if / elseif / else解决方案?还是完全不同的方式来处理这些情况?