package main import ( "fmt" "os" "xml" ) type Email struct { Where string `xml:"attr"` Addr string } type Result struct { XMLName xml.Name `xml:"result"` Name string Phone string Email []Email Groups []string `xml:"group>value"` } const filename = "test2.xml" func main() { oldResult := Result{xml.Name{"", "result"}, "Grace R. Emlin", // name "phone", // no phone given []Email{ Email{"home", "gre@example.com"}, Email{"work", "gre@work.com"}, }, []string{"Friends", "Squash"}, } out, err := os.Create(filename) if err != nil { fmt.Println(err); os.Exit(1) } err = xml.Marshal(out, oldResult) if err != nil { fmt.Println(err); os.Exit(1) } fmt.Fprintf(out, "\n") out.Close() in, err := os.Open(filename) if err != nil { fmt.Println(err); os.Exit(1) } var newResult Result err = xml.Unmarshal(in, &newResult) if err != nil { fmt.Println(err); os.Exit(1) } in.Close() fmt.Printf("oldResult: %v\n", oldResult) fmt.Printf("newResult: %v\n", newResult) fmt.Printf("oldResult: %#v\n", oldResult) fmt.Printf("newResult: %#v\n", newResult) }