我正在嘗試用 golang 讀取一些 XML。我基于這個(gè)有效的例子。https://gist.github.com/kwmt/6135123#file-parsetvdb-go這是我的文件:城堡0.xml<?xml version="1.0" encoding="UTF-8" ?><Channel><Title>test</Title><Description>this is a test</Description></Channel>測試package mainimport ( "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)}輸出: - %!d(string=)有誰知道我做錯(cuò)了什么?(我這樣做是為了學(xué)習(xí)圍棋,所以放輕松:P)
- 1 回答
- 0 關(guān)注
- 408 瀏覽
添加回答
舉報(bào)
0/150
提交
取消