XML/Output: Difference between revisions

Content added Content deleted
No edit summary
(→‎{{header|Go}}: update for new template library, also added an error check)
Line 617: Line 617:


import (
import (
"io"
"bytes"
"fmt"
"os"
"os"
"template"
"template"
Line 633: Line 634:


var tmpl = `<CharacterRemarks>
var tmpl = `<CharacterRemarks>
{{range .}} <Character name="{{xml .Char}}">{{xml .Rem}}</Character>
{.repeated section @}
{{end}}</CharacterRemarks>
<Character name="{Char}">{Rem}</Character>
{.end}
</CharacterRemarks>
`
`


func xmlFormatter(w io.Writer, _ string, value ...interface{}) {
func xmlEscapeString(s string) string {
var b bytes.Buffer
xml.Escape(w, []byte(value[0].(string)))
xml.Escape(&b, []byte(s))
return b.String()
}
}


func main() {
func main() {
xt := template.MustParse(tmpl, template.FormatterMap{"": xmlFormatter})
xt := template.New("").Funcs(template.FuncMap{"xml": xmlEscapeString})
xt.Execute(os.Stdout, crms)
template.Must(xt.Parse(tmpl))
if err := xt.Execute(os.Stdout, crms); err != nil {
fmt.Println(err)
}
}</lang>
}</lang>
Output:
Output: