我刚刚开始学习go,请尝试一下,我想从用户定义的结构创建一个简单的XML文件,但是该程序未输出预期的结果,我的XML中没有id或name节点输出:
package main
import (
"encoding/xml"
"fmt"
"os"
)
//type company struct {
// id int32
// name string
//}
type Company struct {
XMLname xml.Name `xml:"Company"`
id int32 `xml:"id"`
name string `xml:"name"`
}
func main() {
var company = Company{id: 45, name: "toto"}
output, err := xml.MarshalIndent(company, " ", " ")
if err != nil {
fmt.Printf("error: %v\n", err)
}
os.Stdout.Write(output)
}
输出:
<Company>
<Company></Company>
</Company>