To add the correct answer to bgp (+1); changing the function to use the io.Writer parameter as an argument, you can output your XML to any type of output by implementing the io.Writer interface .
func processTopic(w io.Writer, id string, properties map[string][]string) {
fmt.Fprintf(w, "<card entity=\"%s\">\n", id)
fmt.Fprintln(w, " <facts>")
for k, v := range properties {
for _,value := range v {
fmt.Fprintf(w, " <fact property=\"%s\">%s</fact>\n", k, value)
}
}
fmt.Fprintln(w, " </facts>")
fmt.Fprintln(w, "</card>")
}
Screen printing (Stdout):
processTopic(os.Stdout, id, properties)
Writing to a file (code taken from bgp answer):
f, err := os.Create("out.xml")
if err != nil { panic(err) }
defer f.Close()
processTopic(f, id, properties)
source
share