当前位置:网站首页>Go language self-study series | golang standard library encoding/xml

Go language self-study series | golang standard library encoding/xml

2022-06-21 21:28:00 COCOgsta

Video source :B standing 《golang Introduction to project practice [2021 newest Go Language course , There is no nonsense , Dry only ! Ongoing update ...]》

Organize the teacher's course content and test notes while studying , And share it with you , Infringement is deleted , Thank you for your support !

Attach summary sticker :Go Language self-study series | Summary _COCOgsta The blog of -CSDN Blog _go Language self-study


xml Package implementation xml analysis

The two core functions

func Marshal(v interface{}) ([]byte, error)

take struct Code as xml, Can accept any type of

func Unmarshal(data []byte, v interface{}) error

take xml Transcoding into struct Structure

Two core structures

type Decoder struct {
    ...
}

Read from the input stream and parse xml

type Encoder struct {
    // contains filtered or unexpected fields
}

Write xml To the output stream

package main

import (
    "encoding/xml"
    "fmt"
)

type Person struct {
    XMLName xml.Name `xml:"person"`
    Name    string   `xml:"name"`
    Age     int      `xml:"age"`
    Email   string   `xml:"email"`
}

func Marshal() {
    p := Person {
        Name: "tom",
        Age: 20,
        Email: "[email protected]",
    }
    // b, _ := xml.Marshal(p)
    //  Indented format 
    b, _ := xml.MarshalIndent(p, " ", "  ")
    fmt.Printf("%v\n", string(b))
}

func main() {
    Marshal()
}

Running results

[Running] go run "/Users/guoliang/Documents/Source Code/go/test.go"
 <person>
   <name>tom</name>
   <age>20</age>
   <email>[email protected]</email>
 </person>

You can also read and write files

package main

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

type Person struct {
    XMLName xml.Name `xml:"person"`
    Name    string   `xml:"name"`
    Age     int      `xml:"age"`
    Email   string   `xml:"email"`
}

func read() {
    b, _ := ioutil.ReadFile("a.xml")
    var p Person
    xml.Unmarshal(b, &p)
    fmt.Printf("p: %v\n", p)

}

func write() {
    p := Person{
        Name: "tom",
        Age: 20,
        Email: "[email protected]",
    }

    f, _ := os.OpenFile("a.xml", os.O_WRONLY, 0777)
    defer f.Close()
    e := xml.NewEncoder(f)
    e.Encode(p)

}

func main() {
    read()
}

Running results

[Running] go run "/Users/guoliang/Documents/Source Code/go/test.go"
p: {
   { person} tom 20 [email protected]}
package main

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

type Person struct {
    XMLName xml.Name `xml:"person"`
    Name    string   `xml:"name"`
    Age     int      `xml:"age"`
    Email   string   `xml:"email"`
}

func read() {
    b, _ := ioutil.ReadFile("a.xml")
    var p Person
    xml.Unmarshal(b, &p)
    fmt.Printf("p: %v\n", p)

}

func write() {
    p := Person{
        Name: "tom",
        Age: 20,
        Email: "[email protected]",
    }

    f, _ := os.OpenFile("a.xml", os.O_WRONLY, 0777)
    defer f.Close()
    e := xml.NewEncoder(f)
    e.Encode(p)

}

func main() {
    write()
}

Running results

<person><name>tom</name><age>20</age><email>[email protected]</email></person>
原网站

版权声明
本文为[COCOgsta]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211941050554.html