我有这个结构:
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
}
我想以Type依赖的方式显示我的段落。
我发现的唯一解决方案是基于专用功能,例如isAttachment测试Typein Go 和 nested {{if}}:
{{range .Paragraphs}}
{{if .IsAttachment}}
-- attachement presentation code --
{{else}}{{if .IsMenu}}
-- menu --
{{else}}
-- default code --
{{end}}{{end}}
{{end}}
事实上,我有更多类型,这使得它更加奇怪,将 Go 代码与IsSomething函数和模板与那些{{end}}.
什么是干净的解决方案?go 模板中有一些switch或解决方案吗?if/elseif/else还是用完全不同的方式来处理这些案件?