Go XML Unmarshal示例无法编译

go docs中的Xml示例已损坏。有谁知道如何使它工作?当我编译它时,结果是:


xmlexample.go:34: cannot use "name" (type string) as type xml.Name in field value

xmlexample.go:34: cannot use nil as type string in field value

xmlexample.go:34: too few values in struct initializer

以下是相关代码:

package main


import (

        "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 )

}


慕森卡
浏览 250回答 3
3回答

慕尼黑5688855

线var&nbsp;result&nbsp;=&nbsp;Result{&nbsp;"name",&nbsp;"phone",&nbsp;nil&nbsp;}需要成为var&nbsp;result&nbsp;=&nbsp;Result{&nbsp;Name:&nbsp;"name",&nbsp;Phone:&nbsp;"phone",&nbsp;Email:&nbsp;nil&nbsp;}然后它应该按预期工作。我提交了修补程序以修复文档,巧合的是,此后不久就发布了一个版本,因此没有人应该再次遇到这一特定问题。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go