Go实战--go语言中使用YAML配置文件 与json xml ini对比

                       

生命不止,继续 go go go !!!

golang中如何使用json在前面介绍过了:
Go语言学习之encoding/json包(The way to go)

golang中如何使用xml在前面也有介绍过:
Go语言学习之encoding/xml(The way to go)

json使用

JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式。它基于 ECMAScript (w3c制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。

新建一个文件名为conf.json,键入内容:

{    "enabled": true,    "path": "/usr/local"}
  
  
  • 1
  • 2
  • 3
  • 4

新建main.go,键入内容:

package mainimport (    "encoding/json"    "fmt"    "os")type configuration struct {    Enabled bool    Path    string}func main() {    file, _ := os.Open("conf.json")    defer file.Close()    decoder := json.NewDecoder(file)    conf := configuration{}    err := decoder.Decode(&conf)    if err != nil {        fmt.Println("Error:", err)    }    fmt.Println(conf.Path)}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

xml使用

可扩展标记语言,标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言。

新建一个文件名为conf.xml,键入内容:


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值