首页 > golang > Golang xml
2018
07-13

Golang xml

  package main

    import (
        "encoding/xml"
        "fmt"
        "os"
    )

    type Address struct {
        City, State string
    }
    type Person struct {
        XMLName   xml.Name `xml:"person"`
        Id        int      `xml:"id,attr"`
        FirstName string   `xml:"name>first"`
        LastName  string   `xml:"name>last"`
        Age       int      `xml:"age"`
        Height    float32  `xml:"height,omitempty"`
        Married   bool
        Address
        Comment string `xml:",comment"`
    }

    func main() {
        v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
        v.Comment = " Need more details. "
        v.Address = Address{"Hanga Roa", "Easter Island"}

        output, err := xml.MarshalIndent(v,"","    ")
        if err != nil {
            fmt.Printf("error: %vn", err)
        }

        os.Stdout.Write(output)
    }
作者:golang中国
golang中国

本文》有 3263 条评论

留下一个回复