要让数据对象能在网络上传输或存储,我们需要进行编码和解码。现在比较流行的编码方式有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编解码的更多相关文章

  1. 各种音视频编解码学习详解 h264 ,mpeg4 ,aac 等所有音视频格式

    编解码学习笔记(一):基本概念 媒体业务是网络的主要业务之间.尤其移动互联网业务的兴起,在运营商和应用开发商中,媒体业务份量极重,其中媒体的编解码服务涉及需求分析.应用开发.释放 license收费等 ...

  2. 集显也能硬件编码:Intel SDK && 各种音视频编解码学习详解

    http://blog.sina.com.cn/s/blog_4155bb1d0100soq9.html INTEL MEDIA SDK是INTEL推出的基于其内建显示核心的编解码技术,我们在播放高清 ...

  3. 我的Android进阶之旅------>Android中编解码学习笔记

    编解码学习笔记(一):基本概念 媒体业务是网络的主要业务之间.尤其移动互联网业务的兴起,在运营商和应用开发商中,媒体业务份量极重,其中媒体的编解码服务涉及需求分析.应用开发.释放license收费等等 ...

  4. 【miscellaneous】各种音视频编解码学习详解

    编解码学习笔记(一):基本概念 媒体业务是网络的主要业务之间.尤其移动互联网业务的兴起,在运营商和应用开发商中,媒体业务份量极重,其中媒体的编解码服务涉及需求分析.应用开发.释放license收费等等 ...

  5. 【FFMPEG】各种音视频编解码学习详解 h264 ,mpeg4 ,aac 等所有音视频格式

    目录(?)[-] 编解码学习笔记二codec类型 编解码学习笔记三Mpeg系列Mpeg 1和Mpeg 2 编解码学习笔记四Mpeg系列Mpeg 4 编解码学习笔记五Mpeg系列AAC音频 编解码学习笔 ...

  6. iOS8系统H264视频硬件编解码说明

    公司项目原因,接触了一下视频流H264的编解码知识,之前项目使用的是FFMpeg多媒体库,利用CPU做视频的编码和解码,俗称为软编软解.该方法比较通用,但是占用CPU资源,编解码效率不高.一般系统都会 ...

  7. IOS和Android支持的音频编解码

    1.IOS编码 参考文档地址:https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/Multimedi ...

  8. java编解码技术,netty nio

    对于java提供的对象输入输出流ObjectInputStream与ObjectOutputStream,可以直接把java对象作为可存储 的字节数组写入文件,也可以传输到网络上去.对与java开放人 ...

  9. 编解码-marshalling

    JBoss的Marshalling序列化框架,它是JBoss内部使用的序列化框架,Netty提供了Marshalling编码和解码器,方便用户在Netty中使用Marshalling. JBoss M ...

随机推荐

  1. PHP的XML Parser(转)

    PHP处理XML文件 一.读取,更新(创建或者操作)一个XML文档,需要XML解析器 .有两种XML parsers: 1. Tree-based parser:将XML文档转化为DOM Tree结构 ...

  2. SQL中的注释语句

    SQL中的注释分为单行注释和多行注释.顾名思义,单行注释就是对一行进行注释,多行注释就是同时对多行进行注释. 一.单行注释 SQL语句中的单行注释使用 -- create database datab ...

  3. CF 579A (二进制拆分)

    在培养皿中,每个细胞每天会繁殖,数量*2 我们可以在任意天加入任意数量的细胞入培养皿中. 想要知道最少加入多少个细胞,可以使得有一天,培养皿中细胞的数量会恰好为x 其实就是输出X的二进制中有多少个1 ...

  4. Rookey.Frame之菜单设置

    在上一篇博文 Rookey.Frame企业级快速开发框架开源了 中我们介绍了Rookey.Frame极速开发框架的最新更新及开源介绍,后面慢慢介绍该框架的使用方法,本人文笔不好,写得不够好的地方请大家 ...

  5. 【51nod】1655 染色问题

    题解 首先每个颜色出现的次数应该是一样的 \(\frac{C_{n}^{2}}{n} = \frac{n - 1}{2}\) 所以n如果是偶数那么就无解了 然后我们需要让每个点连颜色不同的四条边 只要 ...

  6. Xposed模块开发教程

    转:http://vbill.github.io/2015/02/10/xposed-1/     http://blog.csdn.net/zhangmiaoping23/article/detai ...

  7. 1. python 类型和运算

    类型和运算 (Types and Operations) Introducing Python Object Types 在非正式的意义上, 在 Python 中, 我们用一些东西做事情. " ...

  8. python中%r和%s的区别

    %r用rper()方法处理对象 %s用str()方法处理对象 有些情况下,两者处理的结果是一样的,比如说处理int型对象. 例一: print "I am %d years old.&quo ...

  9. Azure ServiceBus的消息中带有@strin3http//schemas.microsoft.com/2003/10/Serialization/�

    今天碰到一个很讨厌的问题,使用nodejs 接收Azure service bus队列消息的时候,出现了:@strin3http//schemas.microsoft.com/2003/10/Seri ...

  10. Windows Server 2008 R2的web服务器nginx和Apache的比较

    因为很喜欢nginx,所以也想尝试在Windows下使用nginx,前面安装配置都挺顺利,把域名解析尽量后,通过域名代理访问jboss,却异常的慢,起码有3秒的时间才显示页面,而这个页面是jboss的 ...