go docs中的Xml示例已損壞。有誰知道如何使它工作?當(dāng)我編譯它時(shí),結(jié)果是:xmlexample.go:34: cannot use "name" (type string) as type xml.Name in field valuexmlexample.go:34: cannot use nil as type string in field valuexmlexample.go:34: too few values in struct initializer以下是相關(guān)代碼:package mainimport ( "bytes" "xml")type Email struct { Where string "attr"; Addr string;}type Result struct { XMLName xml.Name "result"; Name string; Phone string; Email []Email;}var buf = bytes.NewBufferString ( `<result> <email where="home"> <addr>gre@example.com</addr> </email> <email where='work'> <addr>gre@work.com</addr> </email> <name>Grace R. Emlin</name> <address>123 Main Street</address></result>`)func main() { var result = Result{ "name", "phone", nil } xml.Unmarshal ( buf , &result ) println ( result.Name )}
3 回答

慕尼黑5688855
TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊
線
var result = Result{ "name", "phone", nil }
需要成為
var result = Result{ Name: "name", Phone: "phone", Email: nil }
然后它應(yīng)該按預(yù)期工作。我提交了修補(bǔ)程序以修復(fù)文檔,巧合的是,此后不久就發(fā)布了一個(gè)版本,因此沒有人應(yīng)該再次遇到這一特定問題。
- 3 回答
- 0 關(guān)注
- 291 瀏覽
添加回答
舉報(bào)
0/150
提交
取消