go - Reading XML with golang -
i'm trying read som xml golang. i'm basing on example works. https://gist.github.com/kwmt/6135123#file-parsetvdb-go
this files:
castle0.xml
<?xml version="1.0" encoding="utf-8" ?> <channel> <title>test</title> <description>this test</description> </channel> test.go
package main import ( "encoding/xml" "fmt" "io/ioutil" "os" ) type query struct { chan channel `xml:"channel"` } type channel struct { title string `xml:"title"` desc string `xml:"description"` } func (s channel) string() string { return fmt.sprintf("%s - %d", s.title, s.desc) } func main() { xmlfile, err := os.open("castle0.xml") if err != nil { fmt.println("error opening file:", err) return } defer xmlfile.close() b, _ := ioutil.readall(xmlfile) var q query xml.unmarshal(b, &q) fmt.println(q.chan) } output: - %!d(string=)
any 1 know i'm doing wrong? (i'm doing learn go, go easy on me :p)
other packages, including encoding/json , encoding/xml can see exported data. firstly title , desc should title , desc correspondingly.
secondly, you're using %d (integer) format in sprintf when printing string. that's why you're getting %!d(string=), means "it's not integer, it's string!".
thirdly, there no query in xml, unmarshal directly q.chan.
this working example. http://play.golang.org/p/l0iml2id-j
Comments
Post a Comment