go docs中的Xml示例已損壞。有誰知道如何使它工作?當我編譯它時,結果是: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以下是相關代碼: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>[email protected]</addr> </email> <email where='work'> <addr>[email protected]</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貢獻1848條經驗 獲得超2個贊
線
var result = Result{ "name", "phone", nil }
需要成為
var result = Result{ Name: "name", Phone: "phone", Email: nil }
然后它應該按預期工作。我提交了修補程序以修復文檔,巧合的是,此后不久就發布了一個版本,因此沒有人應該再次遇到這一特定問題。
- 3 回答
- 0 關注
- 290 瀏覽
添加回答
舉報
0/150
提交
取消