package main import ( "bytes" "fmt" "io" ) type node struct { name string children []node attributes map[string]string } func (n node) String() string { buf := new(bytes.Buffer) n.print(buf, " ") return buf.String() } func (n node) print(out io.Writer, indent string) { if len(n.attributes) > 0 { fmt.Fprintf(out, "\n%v%v %s", indent, n.name, n.attributes) } else { fmt.Fprintf(out, "\n%v%v", indent, n.name) } for _, nn := range n.children { nn.print(out, indent+" ") } } func Node(name string) node { return node{name: name, attributes: make(map[string]string)} } func (n node) append(nn ...node) node { n.children = append(n.children, nn...) return n } func (n node) attribute(key string, value string) node { n.attributes[key] = value return n }