Storing User Input in a XML File using Golang

Hey All,

Currently i am trying to parse a XML File, read the contents, create a new xml file asking for user input and then storing that data into the new XML File that i created. Here is what i have so far if someone could help me get started and point me in a good direction that would be awesome. Appreciate any help! Here is the code i have started https://play.golang.org/p/P_c2ZQRnYO

It looks like what you want to use is templating, check out this documentation: https://golang.org/pkg/text/template/

also if you want to see examples of this in use, check out this git repo:

Edit: Now that I look at it there’s a lot there and it can quickly get overwhelming/ drain patience. So let me tell you were to look and what you’ll find that I think will aid you in your project.

I’m assuming you already have experience generating a file, other wise check out file 003 example 02.
file 004 goes over retrieving a template from a file and generating a file.
005 dives into plugging in information in this case just one value
006 starts using variable to plug in information
007 show’s how you can plug in composite types into a template/ how to go through each index

1 Like

Hi Jesse,

first things first: in line 62 you are ignoring the error. Please don’t ignore it, but print it and terminate your program if you encounter an error.

That said, I don’t see any major problem with your code. Maybe you can also try to get the input parameter as a command line argument (use the “flag” package.)

Finally, it’s probably not a great idea to use the “template” package with XML. XML is a binary format, not text.[1]

[1] https://hsivonen.fi/producing-xml/#nottext

3 Likes

That article is a good read.

1 Like

I will change the code to check for the errors and look into the flag package and make sure that i’m not using the template package with XML i didn’t think i was but ill double check , thanks for the help!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.