生命不止,继续 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"}
新建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)}
xml使用
可扩展标记语言,标准通用标记语言