英文:
Go XML Unmarshal example doesn't compile
问题
在go docs中的Xml示例有问题。有人知道如何使其工作吗?当我编译它时,结果是:
xmlexample.go:34: 无法将“name”(类型为string)用作字段值中的xml.Name类型
xmlexample.go:34: 无法将nil用作字段值中的string类型
xmlexample.go:34: 结构初始化器中的值太少
以下是相关的代码:
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 )
}
英文:
The Xml example in the go docs is broken. Does anyone know how to make it work? When I compile it, the result is:
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
Here is the relevant code:
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 )
}
答案1
得分: 4
type Result
被定义为:
type Result struct {
XMLName xml.Name "result"
Name string
Phone string
Email []Email
}
type xml.Name
嵌入在 type Result
中,被定义为:
// Name 代表一个带有命名空间标识符(Space)的 XML 名称(Local)。
// 在 Parser.Token 返回的标记中,Space 标识符以规范 URL 的形式给出,而不是在解析的文档中使用的短前缀。
type Name struct {
Space, Local string
}
因此,使用类似以下的复合字面量进行初始化:
var result = Result{xml.Name{}, "name", "phone", nil}
var result = Result{xml.Name{"space", "local"}, "name", "phone", nil}
var result = Result{Name: "name", Phone: "phone", Email: nil}
英文:
The type Result
is defined as:
type Result struct {
XMLName xml.Name "result"
Name string
Phone string
Email []Email
}
The type xml.Name
, embedded in type Result
, is defined as:
// A Name represents an XML name (Local) annotated
// with a name space identifier (Space).
// In tokens returned by Parser.Token, the Space identifier
// is given as a canonical URL, not the short prefix used
// in the document being parsed.
type Name struct {
Space, Local string
}
Therefore, initialize, using composite literals, using something similar to one of:
var result = Result{xml.Name{}, "name", "phone", nil}
var result = Result{xml.Name{"space", "local"}, "name", "phone", nil}
var result = Result{Name: "name", Phone: "phone", Email: nil}
答案2
得分: 3
这行代码需要修改为:
var result = Result{ Name: "name", Phone: "phone", Email: nil }
然后它应该按预期工作。我提交了一个修复文档的补丁,巧合的是,不久之后就发布了一个新版本,所以再也不会遇到这个特定的问题了。
英文:
The line
var result = Result{ "name", "phone", nil }
needs to become
var result = Result{ Name: "name", Phone: "phone", Email: nil }
Then it should work as expected. I submitted a patch to fix the documentation and by coincidence a release occurred soon after, so no one should run into this particular issue again.
答案3
得分: 1
它也可以在您提供xml.Name{}以及其他参数的情况下正常工作,如下所示:
var result = Result{ xml.Name{"", "result"}, "name", "phone", nil }
英文:
It also works if you supply xml.Name{} along with the other arguments, like so:
var result = Result{ xml.Name{"", "result"}, "name", "phone", nil }
答案4
得分: 0
这里
var result Result
起作用。
英文:
Here
var result Result
works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论