0
A
回答
0
下面是一个示例程序,它使用encoding/json
软件包对一个简单结构进行序列化和反序列化。请参阅代码注释以获得解释。
请注意,我在这里省略了错误处理。
package main
import (
"bytes"
"encoding/json"
"fmt"
)
// your data structure need not be exported, i.e. can have lower case name
// all exported fields will by default be serialized
type person struct {
Name string
Age int
}
func main() {
writePerson := person{
Name: "John",
Age: 32,
}
// encode the person as JSON and write it to a bytes buffer (as io.Writer)
var buffer bytes.Buffer
_ = json.NewEncoder(&buffer).Encode(writePerson)
// this is the JSON-encoded text
// output: {"Name":"John","Age":32}
fmt.Println(string(buffer.Bytes()))
// deocde the person, reading from the buffer (as io.Reader)
var readPerson person
_ = json.NewDecoder(&buffer).Decode(&readPerson)
// output: {John 32}
fmt.Println(readPerson)
}
相关问题
- 1. 与地图[字符串]字符串数据编Golang yaml.v2结构失败
- 2. 字符串的最佳数据结构
- 3. Java树字符串数据结构
- 4. 字符串集合的数据结构
- 5. 字符串索引的数据结构?
- 6. 编组包含字符串的结构
- 7. Golang net.ListenTCP结构
- 8. Golang - 结构
- 9. Golang - 从结构数组中创建一个字符串
- 10. 如何转换Golang中包含其他结构的结构?
- 11. Golang - 字符串与许多分隔符切片或结构
- 12. 更改结构字符串
- 13. C#字符串去结构
- 14. 结构字符串问题
- 15. C字符串结构
- 16. golang结构数组转换
- 17. Golang嵌入结构
- 18. Golang结构比较
- 19. Golang混淆结构
- 20. golang一个结构
- 21. Golang接口结构
- 22. Golang代码结构
- 23. Golang和JSON结构
- 24. golang重用结构
- 25. 泛化结构 - golang
- 26. 与结构与golang
- 27. Golang - 嵌套结构
- 28. Highcharts以x数据,y值golang结构
- 29. 字符* []结构
- 30. 输入字符串的不结构
序列化零值肯定会是最简单的解决方案。 – JimB
可能是其中一个包https://golang.org/pkg/encoding/#pkg-subdirectories – jcbwlkr