package main import ( "encoding/xml" "fmt" ) type Person struct { name string age int } func main() { person := Person{name:"Anyzm",age:18} if data,err := xml.MarshalIndent(person,"", " "); err != nil{ fmt.Println(err) return }else{ fmt.Println(data) } }
输出结果:
[60 80 101 114 115 111 110 62 60 47 80 101 114 115 111 110 62]
MarshalIndent函数输出的是[]byte类型,你的data没有转成string类型