转: gob编解码
要让数据对象能在网络上传输或存储,我们需要进行编码和解码。现在比较流行的编码方式有JSON,XML等。然而,Go在gob包中为我们提供了另一种方式,该方式编解码效率高于JSON。gob是Golang包自带的一个数据结构序列化的编码/解码工具
源和目的地值/类型不需要完全对应。在接收变量中,但从发送类型或值丢失的字段将在目标中被忽略。如果在两个字段中都存在同名的字段,则它们的类型必须兼容。接收器和发送器都会做所有必要的间接和迂回,以在实际值和实际值之间转换。
struct { A, B int }
can be sent from or received into any of these Go types:
struct { A, B int } // the same
*struct { A, B int } // extra indirection of the struct
struct { *A, **B int } // extra indirection of the fields
struct { A, B int64 } // different concrete value type; see below
It may also be received into any of these:
struct { A, B int } // the same
struct { B, A int } // ordering doesn't matter; matching is by name
struct { A, B, C int } // extra field (C) ignored
struct { B int } // missing field (A) ignored; data will be dropped
struct { B, C int } // missing field (A) ignored; extra field (C) ignored.
Attempting to receive into these types will draw a decode error:
struct { A int; B uint } // change of signedness for B
struct { A int; B float } // change of type for B
struct { } // no field names in common
struct { C, D int } // no field names in common
例子:
package main
import (
"bytes"
"encoding/gob"
"fmt"
)
type Person struct {
Name string
Age int
Action Run
}
type Run struct {
Speed int
}
func main() {
var dao bytes.Buffer
var encoder = gob.NewEncoder(&dao)
var decoder = gob.NewDecoder(&dao)
p := Person{Name:"chen",Age:18,Action:Run{80}}
err := encoder.Encode(&p)
if err != nil{
panic(err)
}
fmt.Println(dao.String())
var d Person
err = decoder.Decode(&d)
if err != nil{
panic(err)
}
fmt.Println(d)
}
如果Encode/Decode类型是interface或者struct中某些字段是interface{}的时候,需要在gob中注册interface可能的所有实现或者可能类型,不然会报:panic: gob: type not registered for interface: main.Run错误
例子2 编解码的struct中某些字段是interface{}的时候
package main
import (
"encoding/gob"
"fmt"
"bytes"
)
func init() {
gob.Register(&Run{})//必须在encoding/gob编码解码前进行注册
}
//panic: gob: type not registered for interface: main.Run
type Person struct {
Name string
Age int
Action interface{}
}
type Run struct {
Speed int
}
func main() {
var dao bytes.Buffer
encoder := gob.NewEncoder(&dao)
decoder := gob.NewDecoder(&dao)
p := Person{Name:"chen",Age:18,Action:Run{80}}
err := encoder.Encode(&p)
if err != nil{
panic(err)
}
fmt.Println(dao.String())
var d Person
err = decoder.Decode(&d)
if err != nil{
panic(err)
}
fmt.Println(d)
}
例子3 编解码的类型是interface
package main
import (
"fmt"
"bytes"
"encoding/gob"
)
func init() {
gob.Register(&Person{})//必须在encoding/gob编码解码前进行注册
gob.Register(&Dog{})
}
type Actioner interface {
Action()
}
type Person struct {
Name string
}
type Dog struct {
Name string
}
func (p *Person)Action() {
fmt.Println("person action")
}
func (p *Dog)Action() {
fmt.Println("dog action")
}
func main() {
var dao bytes.Buffer
encoder := gob.NewEncoder(&dao)
decoder := gob.NewDecoder(&dao)
var action Actioner
action = &Person{"chen"}
err := encoder.Encode(&action)
if err != nil{
panic(err)
}
action = &Dog{"jok"}
err = encoder.Encode(&action)
if err != nil{
panic(err)
}
err = decoder.Decode(&action)
if err != nil{
panic(err)
}
fmt.Println(action)
action.Action()
err = decoder.Decode(&action)
if err != nil{
panic(err)
}
fmt.Println(action)
action.Action()
}
我们也可以将*bytes.Buffer换成*os.File,将编码后的对象写入磁盘存储
性能测试
下面进行一下简单的性能测试,测试一下gob和json的编解码性能。
gob:
package main
import (
"bytes"
"encoding/gob"
"fmt"
"time"
)
type Person struct {
Name string
Age int
Action Run
}
type Run struct {
Speed int
}
var dao bytes.Buffer
var encoder = gob.NewEncoder(&dao)
var decoder = gob.NewDecoder(&dao)
func Gob() {
p := Person{Name:"chen",Age:18,Action:Run{80}}
err := encoder.Encode(&p)
if err != nil{
panic(err)
}
//fmt.Println(dao.String())
var d Person
err = decoder.Decode(&d)
if err != nil{
panic(err)
}
//fmt.Println(d)
}
func main() {
now := time.Now()
start := now.UnixNano()
for i := 0; i < 10000; i++ {
Gob()
}
now2 := time.Now()
end := now2.UnixNano()
fmt.Println(end - start) //25016400
}
gob编解码循环10000次所需时间为25016400纳秒
json:
package main
import (
"encoding/json"
"fmt"
"time"
)
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Action Run `json:"action"`
}
type Run struct {
Speed int `json:"speed"`
}
func Json() {
p := Person{Name:"chen",Age:18,Action:Run{80}}
data,err := json.Marshal(p)
if err != nil{
panic(err)
}
//fmt.Println(string(data))
var d Person
err = json.Unmarshal(data,&d)
if err != nil{
panic(err)
}
//fmt.Println(d)
}
func main() {
now := time.Now()
start := now.UnixNano()
for i := 0; i < 10000; i++ {
Json()
}
now2 := time.Now()
end := now2.UnixNano()
fmt.Println(end - start) //45037200
}
json编解码循环10000次所需时间为45037200纳秒
总结:粗略的测试gob的性能大概是json的两倍左右
转: gob编解码的更多相关文章
- 各种音视频编解码学习详解 h264 ,mpeg4 ,aac 等所有音视频格式
编解码学习笔记(一):基本概念 媒体业务是网络的主要业务之间.尤其移动互联网业务的兴起,在运营商和应用开发商中,媒体业务份量极重,其中媒体的编解码服务涉及需求分析.应用开发.释放 license收费等 ...
- 集显也能硬件编码:Intel SDK && 各种音视频编解码学习详解
http://blog.sina.com.cn/s/blog_4155bb1d0100soq9.html INTEL MEDIA SDK是INTEL推出的基于其内建显示核心的编解码技术,我们在播放高清 ...
- 我的Android进阶之旅------>Android中编解码学习笔记
编解码学习笔记(一):基本概念 媒体业务是网络的主要业务之间.尤其移动互联网业务的兴起,在运营商和应用开发商中,媒体业务份量极重,其中媒体的编解码服务涉及需求分析.应用开发.释放license收费等等 ...
- 【miscellaneous】各种音视频编解码学习详解
编解码学习笔记(一):基本概念 媒体业务是网络的主要业务之间.尤其移动互联网业务的兴起,在运营商和应用开发商中,媒体业务份量极重,其中媒体的编解码服务涉及需求分析.应用开发.释放license收费等等 ...
- 【FFMPEG】各种音视频编解码学习详解 h264 ,mpeg4 ,aac 等所有音视频格式
目录(?)[-] 编解码学习笔记二codec类型 编解码学习笔记三Mpeg系列Mpeg 1和Mpeg 2 编解码学习笔记四Mpeg系列Mpeg 4 编解码学习笔记五Mpeg系列AAC音频 编解码学习笔 ...
- iOS8系统H264视频硬件编解码说明
公司项目原因,接触了一下视频流H264的编解码知识,之前项目使用的是FFMpeg多媒体库,利用CPU做视频的编码和解码,俗称为软编软解.该方法比较通用,但是占用CPU资源,编解码效率不高.一般系统都会 ...
- IOS和Android支持的音频编解码
1.IOS编码 参考文档地址:https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/Multimedi ...
- java编解码技术,netty nio
对于java提供的对象输入输出流ObjectInputStream与ObjectOutputStream,可以直接把java对象作为可存储 的字节数组写入文件,也可以传输到网络上去.对与java开放人 ...
- 编解码-marshalling
JBoss的Marshalling序列化框架,它是JBoss内部使用的序列化框架,Netty提供了Marshalling编码和解码器,方便用户在Netty中使用Marshalling. JBoss M ...
随机推荐
- C++之客户消费积分管理系统
之前数据结构课程设计要求做这么一个小程序,现在贴上源码,来和大家进行交流学习,希望大家给出意见和建议 程序以链表为主要数据结构对客户信息进行存储,对身份证号码判断了位数及构成(前十七位为数字,最后一位 ...
- 配置JDK-Java运行环境
1.将Java安装包上传到服务器某目录,如E:\jdk-7u45-windows-x64.exe 2.上传后运行jdk-7u45-windows-x64.exe 3.点击[下一步],后选择[更改],改 ...
- C#和PHP 长整型时间互转
//2018/5/14 16:03:05转换:1526284985 public static double ConvertToDouble(DateTime date) { , , )); var ...
- asp.net 微信公众号源码
需要源码,请加QQ:858-048-581 功能菜单 该源码功能十分的全面,具体介绍如下:1.菜单回复:微信自定义回复.关注时回复.默认回复.文本回复.图文回复.语音回复. 请求回复记录.LBS位置回 ...
- require demo 记录备份
预览地址 http://127.0.0.1:8020/requireDemo/myNEW/index.html 注意 远程的 非模块的 empty: demo2
- 【LOJ】#2587. 「APIO2018」铁人两项
题解 学习了圆方树!(其实是复习了Tarjan求点双) 我又双叒叕忘记了tarjan点双一个最重要,最重要的事情! 就是--假如low[v] >= dfn[u],我们就找到了一个点双,开始建立方 ...
- mysql 删除重复项
DELETE FROM j_rank_rise_record WHERE id NOT IN ( SELECT id FROM ( SELECT * FROM j_rank_rise_record g ...
- CentOS7安装和配置mongodb3.6
(1)安装mongodb 1.参考文档 https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/ 2.创建yum源 #v ...
- poj-1151矩形面积并-线段树
title: poj-1151矩形面积并-线段树 date: 2018-10-30 22:35:11 tags: acm 刷题 categoties: ACM-线段树 概述 线段树问题里的另一个问题, ...
- HDU 4348 To the moon 主席树 在线更新
http://acm.hdu.edu.cn/showproblem.php?pid=4348 以前做的主席树没有做过在线修改的题做一下(主席树这种东西正经用法难道不是在线修改吗),标记永久化比较方便. ...