gomangomango
gomangomango

Reputation: 691

Instead of printing XML output, create XML file

In this Go / Golang code, I am printing XML. But instead of doing that, how can I create an XML file with this output?

The reason I want to do this is that the XML output is quite large and instead of copying and pasting the output from the terminal, since it would take quite a long time to highlight it all, it would be best if it was written to an XML file.

Here is the code:

    fmt.Printf("<card>\n")
    fmt.Printf("<title>"%s"</title>\n", properties["/type/object/name"])
    fmt.Printf("https://usercontent.googleapis.com/freebase/v1/image"%s"\n", id)
    fmt.Printf("<text>%s</text>\n", properties["/common/document/text"])
    fmt.Println("<facts>")
    for k, v := range properties {
        for _,value := range v {
            fmt.Printf("<fact property=\"%s\">%s</fact>\n", k, value)
        }
    }
    fmt.Println("</facts>")
    fmt.Println("</card>")

Upvotes: 0

Views: 294

Answers (1)

gomangomango
gomangomango

Reputation: 691

As mentioned in the comments one can create a file using os.Create() like so:

file, _:=os.Create("file.extension")

file being the variable the file is assigned to.

Then one can continually write to the file using:

fmt.Fprintf(file, "text in file")

Upvotes: 1

Related Questions