当前位置:网站首页>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>边栏推荐
- Intersection of vector and plane
- [CTF] attack and defense world Misc
- [专利与论文-19]:江苏省南京市2022年电子信息申报通知(中、高级)
- Tencent global digital ecology Conference - high speed intelligent computing special session!
- MySQL数据库---数据库基础
- What are some tricks that novice programmers don't know?
- js的对象操作(与c的对象比较简单的多)
- Cylinder function block (FB) of PLC function block series
- 十一、美化界面
- unity动态读取外部音乐并播放
猜你喜欢

向量與平面交點

11、 Beautify the interface

Citus 11 for Postgres 完全开源,可从任何节点查询(Citus 官方博客)

Uibutton implements left text and right picture

Cluster 2 - LVS load balancing cluster Dr mode
![[server data recovery] a case of RAID5 data recovery of an EMC server](/img/cc/23adaa1f8bc57d350e4a5647ff9296.jpg)
[server data recovery] a case of RAID5 data recovery of an EMC server

全新混合架构iFormer!将卷积和最大池化灵活移植到Transformer

PowerPoint tutorial, how to organize slides into groups in PowerPoint?

Delaying patient self-help guide | "I have 100 excuses for not delaying, but I am not willing to take a step"

【MySQL·水滴计划】第三话- SQL的基本概念
随机推荐
[microservices 7] in depth analysis of bestavailablerule source code of ribbon load balancing strategy
PC e-commerce platform - search module
科研漫画 | 看图可以学脑电,来试试?
Asynchronous method understanding (demo with code)
ARP协议及ARP攻击
测评 | 在生活中,你是一位什么样的人呢?
Implementation principle of global load balancing
How to solve the problem of automatically updating the click times of weaving dream article list
Xmind8 latest cracking tutorial (useful for personal testing)
Go语言自学系列 | golang标准库encoding/xml
Principle and application of user mode hot patch
evaluating expression ‘ew. sqlSegment != null and ew. sqlSegment != ‘‘ and ew. Mybats plus problems
智力题整理总结
如何解决织梦文章列表自动更新点击次数
On the charm of code language
Cluster I -- LVS load balancing cluster NAT mode and LVS load balancing actual deployment
About n before variables in SQL server and other usage analysis
Xcode plug-in management tool Alcatraz
【物联网开发】正点原子STM32战舰v3+机智云AIoT+APP控制
总结了嵌入式开发中几种常见的设备通信协议