Go言語で構造体をXMLに変換するsnippet
package main import ( "bytes" "encoding/xml" ) type Person struct { Name string Age int Friend *Person } func main() { friend := Person{"john", 40, nil} person := Person{"paul", 71, &friend} var buf bytes.Buffer buf.Write([]byte(xml.Header)) b, _ := xml.Marshal(person) buf.Write(b) println(buf.String()) }
出力
<?xml version="1.0" encoding="UTF-8"?> <Person><Name>paul</Name><Age>71</Age><Friend><Name>john</Name><Age>40</Age></Friend></Person>