golang中encoding / gob和encoding / json之间的区别 [英] difference between encoding/gob and encoding/json in golang

查看:807
本文介绍了golang中encoding / gob和encoding / json之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个GO应用程序,它使用编码/ gob在节点之间通过UDP发送结构和切片。它工作正常,但我注意到,编码/ JSON也有类似的API。搜索并找到这些信息( https://golang.org/pkg/encoding/ ): p>

I am writing an application in GO which uses encoding/gob to send structures and slices over UDP between nodes. It works fine but I notice that encoding/json also has the similar API. Searched and found this information(https://golang.org/pkg/encoding/):


包gob管理gobs流 - 二进制值在编码器(发送器)和解码器(接收器)之间交换
。 >
json包json实现了
RFC 4627中定义的JSON的编码和解码。

gob Package gob manages streams of gobs - binary values exchanged between an Encoder (transmitter) and a Decoder (receiver).
json Package json implements encoding and decoding of JSON as defined in RFC 4627.

有人可以解释对我来说,一个人是否比另一个更有效率,并且总体上比较何时选择什么?此外,如果我需要与非golang应用程序接口,我想json会是首选?

Can someone explain to me whether one is more efficient than the other and in general compare when to choose what? Also if I need to interface with a non-golang application, I guess json would be preferred?

推荐答案

Gob更受欢迎在Go程序之间进行通信时。但是,目前仅在Go中支持gob,并且 C 只支持gob当你确定没有任何其他编程语言编写的程序会尝试解码这些值。

Gob is much more preferred when communicating between Go programs. However, gob is currently supported only in Go and, well, C, so only ever use that when you're sure no program written in any other programming language will try to decode the values.

至于性能,至少在我的机器上,Gob远远超过JSON。 测试文件(在您的GOPATH下自行放置在一个文件夹中)

When it comes to performance, at least on my machine, Gob outperforms JSON by a long shot. Test file (put in a folder on its own under your GOPATH)

$ go test -bench=.        
testing: warning: no tests to run
BenchmarkGobEncoding-4           1000000              1172 ns/op
BenchmarkJSONEncoding-4           500000              2322 ns/op
BenchmarkGobDecoding-4           5000000               486 ns/op
BenchmarkJSONDecoding-4           500000              3228 ns/op
PASS
ok      testencoding    6.814s

这篇关于golang中encoding / gob和encoding / json之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆