difference between encoding/gob and encoding/json

huangapple go评论95阅读模式
英文:

difference between encoding/gob and encoding/json

问题

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

> 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.

有人能解释一下其中一个比另一个更高效,并在一般情况下比较何时选择哪个吗?另外,如果我需要与非Go应用程序进行接口交互,我猜json会更好一些?

英文:

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 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.

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-Go application, I guess json would be preferred?

答案1

得分: 28

Gob在Go程序之间进行通信时更受青睐。然而,目前只有Go和C(https://code.google.com/archive/p/libgob/)支持gob,所以只有在确保没有任何其他编程语言编写的程序尝试解码值时才使用它。

就性能而言,至少在我的机器上,Gob远远优于JSON。测试文件(https://gist.github.com/thehowl/85057ad21697160e81ac42bf358a322a)(将其放在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
英文:

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.

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

答案2

得分: 13

包encoding/gob基本上是Go特定的,不能与其他语言一起使用,但它非常高效(快速且生成的数据较小),可以正确地编组和解组更多的数据结构。通过JSON与其他工具进行接口交互通常更容易。

英文:

Package encoding/gob is basically Go specific and unusable with other languages but it is very efficient (fast and generates small data) and can properly marshal and unmarshal more data structures. Interfacing with other tools is often easier via JSON.

huangapple
  • 本文由 发表于 2016年12月16日 15:39:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/41179453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定